This question already has an answer here:
I am currently working on a project that requires are very simple countdown timer, that works in the tkinter GUI and that dosen't rely on a recursion. I have tried different things but nothing seems to work so far.
import time
from tkinter import *
root = Tk()
root.title("Timer")
root.geometry("100x100")
def countdown(count):
label = Label(root, text= count)
label.place(x=35, y=15)
for i in range(5,0,-1):
countdown(i)
time.sleep(1)
root.mainloop()
You can't use
sleep
because it stopsmainloop
and program can't work. You can useroot.after
to call function after 1000ms (1s)Similar principle as furas's solution already posted, but using a StringVar:
This can be made more elegant if the pieces live in the same class (namely eliminate the need for the
lambda
), but I think you can get the point here.