【发布时间】:2020-07-06 18:48:11
【问题描述】:
对于我的游戏,我需要让用户选择一个难度来开始游戏,但我很难获得当运行下面的代码而不是从我的按钮获得正确的返回时,按钮消失并且我返回无。这是我的代码:
def start_game():
root = tk.Tk()
root.attributes("-topmost", True)
root.withdraw()
answer = messagebox.askyesno("Welcome to Snake!", "Would you like to play?")
if answer == True:
root.deiconify()
mainLabel = tk.Label(root, text='Choose a dificulty: ')
def easy_difficulty():
print('easy')
return 1
def medium_difficulty():
print('medium')
return 5
def hard_difficulty():
print('hard')
return 10
easy_ask = tk.Button(root, text='Easy', command=easy_difficulty())
medium_ask = tk.Button(root, text='Medium', command=medium_difficulty())
hard_ask = tk.Button(root, text='Hard', command=hard_difficulty())
mainLabel.pack()
easy_ask.pack(side=tk.LEFT)
medium_ask.pack(side=tk.LEFT)
hard_ask.pack(side=tk.LEFT)
root.destroy()
root.mainloop()
【问题讨论】:
-
请提供minimal reproducible example。我不确定这是否是唯一的问题,但您可能不希望
command=easy_difficulty()中的括号。
标签: python python-3.x button tkinter messagebox