Reputation: 1
I am learning Python and still in level beginner. I try to make timer program which the timer will be repeated continuously after certain of time.
I write as follow:
from tkinter import *
import threading
import time
mgui = Tk()
def refresh():
threading.Timer(20, refresh).start()
detik = 19
while detik >= 0:
menit,detik = divmod (detik,60)
timer ='{:02d}:{:02d}'.format(menit, detik)
time.sleep(1)
detik -= 1
refresh_waktu=Entry(mgui, width= 6, font=("Helvetica bold", 40), fg='red', bd=2)
refresh_waktu.place(x=155, y=152)
refresh_waktu.insert(END, str(f"{timer:>6}"))
refresh()
mgui.geometry('450x450')
mgui.title('Test')
mgui.mainloop()
When I run the program, the interface GUI seem delay about 20 seconds although after that the program timer running as my expected.
I try to make the Interface also appear when start the program, but I still fail to do so.
Please if anyone can help me to give some advice or suggestion.
I sincerely thank for your help.
Best regards
Upvotes: 0
Views: 309
Reputation: 54867
Tk, like all GUI systems, is event driven. When you create or modify a window, nothing visible happens. All that does is send messages. It is the job of the main loop to extract and dispatch those messages, and that's where the drawing is done. In your case, you are blocking for 20 seconds before you enter the main loop, so nothing will be drawn until then. You need to use something like mgui.after
to request a callback after a second, and use that for your timing.
Upvotes: 1