为什么按下按钮后我的 Tkinter 标签没有显示?
Why is my Tkinter label not showing up after I press a button?
我一直在尝试在用户单击 QuitGame 按钮时显示带有文本“感谢您玩”等的标签。我试图通过在 MainMenuQuit 函数下包含这个标签(称为 QuitLabel)来做到这一点,以便在整个根被销毁之前,消息在界面上显示 3 秒。
但是,按下按钮时不会显示 2 个标签,尽管其余功能可以正常工作。
这是我的代码:
import time
import tkinter
root = Tk()
root.title("Menu")
root.geometry("1000x800")
root.configure(bg = "#339933")
root.resizable(True, True)
def MainMenuQuit():
Label(root, text = "", bg = "#339933").pack()
QuitLabel = Label(root, text = "Thank you for playing, come back soon!", fg = "blue", bg = "#339933").pack()
time.sleep(3)
root.destroy()
button1 = Button(root, text = "Quit Game", command = MainMenuQuit, width = "20", height = "3").pack()
root.mainloop()
函数:
time.sleep(3)
将暂停所有排队的活动,包括 GUI 更新。
您可以强制 root 执行挂起的命令:
root.update_idletasks() # Place before time.sleep()
这将更新 GUI。
我一直在尝试在用户单击 QuitGame 按钮时显示带有文本“感谢您玩”等的标签。我试图通过在 MainMenuQuit 函数下包含这个标签(称为 QuitLabel)来做到这一点,以便在整个根被销毁之前,消息在界面上显示 3 秒。 但是,按下按钮时不会显示 2 个标签,尽管其余功能可以正常工作。 这是我的代码:
import time
import tkinter
root = Tk()
root.title("Menu")
root.geometry("1000x800")
root.configure(bg = "#339933")
root.resizable(True, True)
def MainMenuQuit():
Label(root, text = "", bg = "#339933").pack()
QuitLabel = Label(root, text = "Thank you for playing, come back soon!", fg = "blue", bg = "#339933").pack()
time.sleep(3)
root.destroy()
button1 = Button(root, text = "Quit Game", command = MainMenuQuit, width = "20", height = "3").pack()
root.mainloop()
函数:
time.sleep(3)
将暂停所有排队的活动,包括 GUI 更新。
您可以强制 root 执行挂起的命令:
root.update_idletasks() # Place before time.sleep()
这将更新 GUI。