Reputation: 1
Look at the codes!
root = Tk()
frame = Frame(root)
labelText = StringVar()
label = Label(frame, textvariable=labelText)
labelText.set("Connecting to the server...")
def welcome_note():
time.sleep(5)
labelText.set("Welcome!")
welcome_note()
label.pack()
frame.pack()
root.mainloop()
When executing the code it should be as "connecting server" then after 5 seconds it should show "welcome"
But it is executing only "welcome" after 5 seconds...
Upvotes: 0
Views: 531
Reputation: 3455
Here this should fix your problem. It happened because your label didn't know about the updated information. So adding these lines will show the changes.
Use this link for more information.
from tkinter import *
import time
root = Tk()
frame = Frame(root)
labelText = StringVar()
labelText.set("Connecting to the server...")
label = Label(frame, textvariable=labelText)
label.pack() # ADD THIS
frame.pack() # ADD THIS
label.update() # ADD THIS
def welcome_note():
time.sleep(5)
labelText.set("Welcome!")
label.pack()
frame.pack()
welcome_note()
root.mainloop()
Upvotes: 0
Reputation: 4526
Use the method after
to call welcome_note
after 5 seconds
def welcome_note():
labelText.set("Welcome!")
root = Tk()
frame = Frame(root)
labelText = StringVar()
label = Label(frame, textvariable=labelText)
labelText.set("Connecting to the server...")
label.pack()
frame.pack()
# Calls welcome_note after 5 seconds
root.after(5000, welcome_note)
root.mainloop()
Upvotes: 1