0

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

3
  • An f-string (f"{timer:>6") is a string. There is no point in calling str with it. Commented Feb 21, 2022 at 1:33
  • You have a threading.Timer(20, refresh).start() Commented Feb 21, 2022 at 1:38
  • Thank for your feedback and advice, but I was not clear enough, I learn threading.Timer for repeating a function after delay certain time. It work but the GUI windows delay 20 second. As Mr. Tim's suggestion I have to mgui.after, I try to learn it. Commented Feb 21, 2022 at 2:18

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

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.