简体   繁体   中英

Why is my Tkinter label not showing up after I press a button?

I've been trying to display a label with the text "thank you for playing" etc when the user clicks the QuitGame button. I have attempted to do so by including this label (called QuitLabel) under the MainMenuQuit fuction so that the message is displayed on the interface for 3 seconds before the entire root is destroyed. However, the 2 labels are not displayed when the button is pressed, though the rest of the function works fine. Here is my code:


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()

The function:

time.sleep(3)

will suspend all queued activities, including GUI updates.

You can force root to execute the pending commands with:

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

which will update the GUI.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM