我正在尝试学习 tkinter,我想编写一个简单的石头剪刀布游戏,其中有一个带有 3 个按钮和一个文本小部件的窗口。
我希望能够按下任何按钮并使消息显示在文本字段中,然后单击不同的按钮,文本字段清除并显示与第二个按钮关联的新消息,依此类推。
从我看过的教程中,我知道我可以将函数外壳文本小部件作为按钮命令参数中的参数传递。我知道我可以使用文本字段创建 3 个函数,每个按钮一个(一次显示一个)但这可能不是正确的方法。这是我到目前为止所拥有的:
import tkinter as tk
root = tk.Tk()
root.title("Rock Paper Scissors")
root.geometry("420x200")
def Rock():
rockText = "Paper!"
return rockText
def Paper():
paperText = "Scissors!"
return paperText
def Scissors():
scissorsText = "Rock!"
return scissorsText
def display():
textDisplay = tk.Text(master = root, height = 10, width = 50)
textDisplay.grid(row = 1, columnspan = 5)
textDisplay.insert(tk.END, Rock())
buttonRock = tk.Button(text = "Rock", command = display).grid(row = 0, column = 1, padx = 10)
buttonPaper = tk.Button(text = "Paper").grid(row = 0, column = 2, padx = 10)
buttonScissors = tk.Button(text = "Scissors").grid(row = 0, column = 3, padx = 10)
root.mainloop()
任何帮助将不胜感激。
编辑:第二个想法 - 我可以想象我试图强迫游戏以这种方式工作,这让我自己变得复杂了。使用随机模块,我可以使用一个列表功能为计算机选择,并将随机选择保存在参数中,然后将值返回到显示函数中。
因此,如果我做对了,您只想单击按钮更改文本小部件中的文本。为此,您有两个简单且非常相似的选择。首先是像您一样定义 3 个函数,然后让它们直接更改文本。第二种选择是创建一个函数,根据给定的内容更改文本。请注意,在第二种情况下,我们将不得不使用lambda
which 在较小的项目中效果很好,但当程序变大时会降低程序的效率。
第一个选项:
import tkinter as tk
class App:
def __init__(self):
root=tk.Tk()
root.title("Rock Paper Scissors")
root.geometry("420x200")
self.text=Text(root)
self.text.grid(row=1,columnspan=5)
tk.Button(root,text="Rock",command=self.Rock).grid(row=0,column=1,padx=10)
tk.Button(root,text="Paper",command=self.Paper).grid(row=0,column=2)
tk.Button(root,text="Scissors",command=self.Scissors).grid(row=0,column=3,padx=10)
root.mainloop()
def Rock(self):
text="Paper!"
self.text.delete(0,END) #delete everything from the Text
self.text.insert(0,text) #put the text in
def Paper(self):
text="Scissors!"
self.text.delete(0,END) #delete everything from the Text
self.text.insert(0,text) #put the text in
def Scissors(self):
text="Rock!"
self.text.delete(0,END) #delete everything from the Text
self.text.insert(0,text) #put the text in
if __name__=='__main__':
App()
第二种选择:
import tkinter as tk
class App:
def __init__(self):
root=tk.Tk()
root.title("Rock Paper Scissors")
root.geometry("420x200")
self.text=Text(root)
self.text.grid(row=1,columnspan=5)
tk.Button(root,text="Rock",command=lambda: self.updateText('Paper!')).grid(row=0,column=1,padx=10)
tk.Button(root,text="Paper",command=lambda: self.updateText('Scissors!')).grid(row=0,column=2)
tk.Button(root,text="Scissors",command=lambda: self.updateText('Rock!')).grid(row=0,column=3,padx=10)
root.mainloop()
def updateText(self,text):
self.text.delete(0,END) #delete everything from the Text
self.text.insert(0,text) #put the text in
if __name__=='__main__':
App()
我这里的一些小笔记:
grid
, pack
or place
,则不会将小部件分配给变量,而是将grid
,pack
或place
函数的返回值分配给None
. 因此,而是首先将小部件分配给一个变量,然后像我为Text
-widget所做的那样在其上使用几何管理器。className
-argument设置它Tk
。Tk
打开了多个 -窗口,它会自动选择一个可能不是你想要的。我希望它有帮助。玩得开心编程!
编辑:我刚刚看到您对随机模块的编辑。在这种情况下,我会推荐第二个选项。只需text
从中删除-argumentupdateText
并替换lambda: self.updateText(...)
为self.updateText()
. 就updateText
其本身而言,您添加了您提到的随机列表。:D
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句