SmolBrein
SmolBrein

Reputation: 15

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

Upvotes: 1

Views: 177

Answers (1)

figbeam
figbeam

Reputation: 7176

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.

Upvotes: 1

Related Questions