0

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...

2 Answers 2

1

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()
Sign up to request clarification or add additional context in comments.

Comments

0

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

2 Comments

May I know, why label.update_idletasks() is added?
Hey Komal I forgot to change it. Please check the updated code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.