ARJhe / tkinter_tutorial

tutorial of tkinter
0 stars 0 forks source link

timer/Thread #3

Open ARJhe opened 4 years ago

ARJhe commented 4 years ago

How to create a Timer count in a one main thread with start, pause, resume and reset function?

class Clock():
    global btn_text, conti
    def __init__(self):
        self.init_time = t.time()
        self.timer()

    def timer(self):
        recall = Timer(1.0, self.timer)
        if conti:
            start['text'] = 'stop'
            time_elape = t.time() - self.init_time
            label['text']= self.formater(time_elape)
            recall.start()
        elif not conti:
            recall.cancel()

    def formater(self,sec):
        _hour = math.floor(sec/3600)
        _min = math.floor(sec/60)
        _sec = math.floor(sec - (_min*60) - (_hour*3600))
        return '%02d hour %02d min %02d sec' % (_hour,_min,_sec)

This create a thread by 1 sec.

ARJhe commented 4 years ago

Reference

https://stackoverflow.com/questions/15729498/how-to-start-and-stop-thread