-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtimer.py
66 lines (53 loc) · 1.91 KB
/
timer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import threading
import time
# desc: contains the countdown mechanism that will be used in the timer gui
class Timer:
def __init__(self, mins):
self.mins = mins
self.sec = 0
# used to restore min value to original (restart action)
self.mins_copy = mins
self.timer_pause = False
self.timer_stop = False
self.timer_restart = False
self.timer_event = threading.Event()
self.timer_thread = threading.Thread(target=self.start_timer, daemon=True)
self.timer_thread.start()
def start_timer(self):
# continue the timer until user stops, pauses, or restarts the timer
# and when the timer has not finished
while not self.timer_stop and (self.mins > 0 or self.sec > 0):
if self.mins <= 0 and self.sec <= 0:
break
if self.timer_pause:
self.timer_event.wait()
self.timer_event.clear()
else:
if self.timer_restart:
self.mins = self.mins_copy
self.sec = 0
self.timer_restart = False
else:
if self.sec <= 0:
if self.mins - 1 >= 0:
self.mins -= 1
self.sec = 60
else:
self.sec -= 1
time.sleep(1)
def restart_timer(self):
self.timer_restart = True
def stop_timer(self):
# ends the timer thread
self.timer_stop = True
def pause_timer(self):
# pauses the timer thread
self.timer_pause = True
def resume_timer(self):
# continues the timer thread (if pause was initiated)
self.timer_pause = False
self.timer_event.set()
def get_min_value(self):
return self.mins
def get_sec_value(self):
return self.sec