0

The code i tried to accomplish my result:

from tkinter import*
import time
root=Tk()
lyrics=StringVar()
lyrics.set("unleash")
l=Label(root,textvariable=lyrics)
l.pack()
def change_text():
    lyrics.set("you!!")
    print('ses')


time.sleep(6.9)
change_text()
mainloop()

The problems in this are like the window is displayed after 10 sec. If I shift the mainloop() before the time.sleep() function the time.sleep() executes after the app is closed. The reason is simple that I didn't use after() function because the after() function doesn't accepts the sec value in decimal. Any help is appreciated

rishi
  • 3
  • 3
  • `after` takes an argument in milliseconds, when can easily give you fractional seconds. 6.9 seconds is 6900 milliseconds. – Bryan Oakley Jan 09 '23 at 07:08

2 Answers2

2

The after() function takes the delay argument in milliseconds, where you would pass 6900 instead of 6.9. See: https://www.tcl.tk/man/tcl8.4/TclCmd/after.html

The problem is that mainloop() blocks further execution. You can always use a background thread:

def bg_thread():
    time.sleep(6.9)
    change_text()

thread = threading.Thread(target=bg_thread)
thread.start()

mainloop()

Threading, of course, brings with it a whole host of challenges. It's a good idea to make sure you know what you're doing when working with threads.

See also:

https://docs.python.org/3/library/threading.html#threading.Thread

Tkinter understanding mainloop

When do I need to call mainloop in a Tkinter application?

Sivasankaran K B
  • 322
  • 3
  • 10
  • `tkinter` doesn't always play nice with `threading` so it's recommended that you only call `tkinter` methods from the thread where you created the `tk.Tk`. This solution can cause bigger problems in the future if OP isn't really careful. – TheLizzard Jan 09 '23 at 10:51
-1
import time
def update_label():

   ///update label text here

    label.config(text="New label text")
/// wait for 5 seconds before updating the label

time.sleep(5)
update_label()
acw1668
  • 40,144
  • 5
  • 22
  • 34