【问题标题】:Why is my Tkinter label not showing up after I press a button?为什么我按下按钮后我的 Tkinter 标签不显示?
【发布时间】:2021-01-31 17:13:46
【问题描述】:

当用户单击 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()

【问题讨论】:

    标签: python python-3.x user-interface tkinter python-3.7


    【解决方案1】:

    功能:

    time.sleep(3)
    

    将暂停所有排队的活动,包括 GUI 更新。

    您可以强制 root 执行挂起的命令:

    root.update_idletasks()  # Place before time.sleep()
    

    这将更新 GUI。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-02-27
      • 2017-06-06
      • 1970-01-01
      • 2016-06-30
      • 1970-01-01
      • 2014-05-18
      相关资源
      最近更新 更多