python - Making a Tkinter countdown timer update actively -


this question has answer here:

i'm trying make tkinter widget actively update. please note, countdown() called first!! problem once enter duration countdown, closes old widget should(not seen here). however, new widget, has countdown timer in it, not appear. if hit stop in ide, widget pop up, how time left when process stopped.

def update(start, finish):     timeremaining = finish - start     timedisplay = label(text = "%s" % timeremaining)     timedisplay.pack(side = bottom)     while true:         #check = timeremaining.timetuple()         timeremaining = str(finish - datetime.datetime.now())         timedisplay.config(text=timeremaining)         timedisplay.pack()         time.sleep(.01)   def countdown(duration):     root.destroy()     title = label(text = "countdown")     title.pack()     duration = float(duration)     print(duration)     start = datetime.datetime.now()     starthours =  int(duration / 60)     startminutes = int(duration % 60)     startseconds = (((duration%60) - startminutes)*60)     finish = start + datetime.timedelta(hours = starthours, minutes = startminutes, seconds = startseconds)     update(start, finish) 

i hope gave enough information. if there's in here doesn't make sense without context, let me know.

any ideas? thanks.

i modified how create timer using tkinter? want

# python 3.x use 'tkinter' rather 'tkinter' import tkinter tk import datetime import math minute = 60 hour = 60*minute class app():     def __init__(self):         self.root = tk.tk()         self.done_time=datetime.datetime.now() + datetime.timedelta(seconds=hour/2) # half hour         self.label = tk.label(text="")         self.label.pack()         self.update_clock()         self.root.mainloop()      def update_clock(self):         elapsed = self.done_time - datetime.datetime.now()         h,m,s,fractional_s = elapsed.seconds/3600,elapsed.seconds/60,elapsed.seconds%60         fractional_seconds = math.floor(elapsed.microseconds/1000000.0*100)         self.label.configure(text="%02d:%02d:%02d.%02d"%(h,m,s))         self.root.after(1000, self.update_clock)  app=app() 

(i put fractional_seconds in case needed them ...)


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -