Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Asif Choudhury
2,086 PointsTkinter: how to update a label at a regular interval?
I am trying to update the label (referred to as lbl in the code below inside the while loop) every second. The problem is label is not updating. The code is not throwing any error message as well.
from tkinter import * import tkinter
TIMER_START_MIN=5 TIMER_START_SEC=5*60 min,sec=divmod(TIMER_START_SEC,60) banner_text="{:02d}:{:02d}".format(min,sec)
def timer_func(): global reset_button global lbl global TIMER_START_SEC global banner_text reset_button.config(state=tkinter.DISABLED)
while(TIMER_START_SEC>0):
mins,secs=divmod(TIMER_START_SEC,60)
banner_text="{:02d}:{:02d}".format(min,sec)
lbl.config(text=banner_text)
TIMER_START_SEC=TIMER_START_SEC-1
root=Tk()
top_frame=Frame(root) top_frame.pack(side=TOP)
bottom_frame=Frame(root) bottom_frame.pack(side=BOTTOM)
lbl=Label(top_frame,text=banner_text,font=('Helvetica', 36), fg='black') lbl.grid(row=0,column=0)
start_stop_button=Button(bottom_frame,text="START",font=("Helvetica",24),command=timer_func) start_stop_button.grid(row=0,column=0)
reset_button=Button(bottom_frame,text="RESET",font=("Helvetica",24)) reset_button.grid(row=0,column=1)
root.mainloop()
I tried StringVar, textvariable nothing worked for me.