-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimer.py
72 lines (59 loc) · 1.8 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
67
68
69
70
71
72
import urwid
import time
timerBar = urwid.ProgressBar('timer_bg', 'timer_fill')
class timerHandle():
def __init__(self):
self.handle = None
theHandle = timerHandle()
#this method will update the timer every second and update the handle
# necessary to stop the timer
def update_timer(loop, user_data=0):
timeElapsed = user_data #time elapsed in seconds
totalProgress = (timeElapsed/28800) * 100.0 # total progress on eight hours
if totalProgress > 100.0:
totalProgress = 100.0
timerBar.set_completion(totalProgress)
theHandle.handle = loop.set_alarm_at(time.time()+1, update_timer, user_data=timeElapsed+1)
# This method will remove the ticking timer, and reset the timer
# requires button.loop to be set to a handle to the main loop
def reset_timer(button, user_data=None):
if theHandle.handle != None:
timerStop = button.loop.remove_alarm(theHandle.handle)
if not timerStop:
raise RuntimeError("Failed to stop timer on reset")
timerBar.set_completion(0.0)
# initial kick start from the start button,
# requires button.loop to be set to a handle to the main loop
def start_timer(button, user_data=None):
update_timer(button.loop, user_data=0)
timerStartButton = urwid.Button(
label='Start',
on_press=start_timer,
)
timerResetButton = urwid.Button(
label='Reset',
on_press=reset_timer,
)
timerStartAttr = urwid.AttrMap(
timerStartButton,
'start_button',
'start_button_focus'
)
timerResetAttr = urwid.AttrMap(
timerResetButton,
'reset_button',
'reset_button_focus'
)
buttonColumns = urwid.Columns(
widget_list=[
timerStartAttr,
timerResetAttr,
]
)
timerColumns = urwid.Columns(
widget_list=[
timerBar,
buttonColumns
]
)
timer = urwid.LineBox(timerColumns)