简体   繁体   中英

Tkinter 60 seconds timer

i am trying to do a 60 timer for a project but i am stuck here. My variable temp doesnt update itself on my window everytime i go trough it. Anyhelp would be great.

import tkinter

root=tkinter.Tk()
root.geometry("300x250")
root.title("Time Counter")

Frame=tkinter.Frame(root)
Frame.pack(side=tkinter.TOP)

Label1=tkinter.Label(Frame,text="Timer")
Label1.pack(side=tkinter.LEFT)

def timer(*args,**kwargs):
    temp=int(temps.get())
    while temp>-1:
        temps.set(str(temp))
        root.update()
        time.sleep(1)
        temp-=1

temps=tkinter.StringVar()
temps.set("60")
temps.trace("w",timer)
label=tkinter.Label(Frame,textvariable=temps)
label.pack(side=tkinter.LEFT)
label.after(1000,timer)

root.mainloop()

It works fine if you just remove trace and after and call the function directly:

temps=tkinter.StringVar()
temps.set("60")
label=tkinter.Label(Frame,textvariable=temps)
label.pack(side=tkinter.LEFT)
timer()

Adding the timer() function to the trace of your StringVar leads to the function being called whenever the value of temps changes. This can't work since the function changes the value, itself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM