-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimer.py
64 lines (47 loc) · 1.61 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
"""
This module implements a simple timer/stopwatch.
Save the Timer class in a timer.py module, import that module, and use the
timer object as shown in the sample TimerApp. You can use this approach to
display a timer value and/or to handle periodic events.
@author: kvlinden
@date: Summer, 2016
@date: Spring, 2021 - ported to GuiZero
@author: ka37
@date: Spring 2021 - separated out the model
"""
from guizero import App, PushButton ##Text
from datetime import datetime
class Timer:
def __init__(self):
self.reset()
def reset(self):
self.start_time = datetime.now()
def get_time(self):
time_since_start = datetime.now() - self.start_time
return time_since_start.total_seconds()
"""
class TimerApp:
def __init__(self, app):
app.height = 100
# Set up the timer.
self.timer = Timer()
self.text = Text(app)
# Add reset button.
PushButton(app, command=self.reset, text='Reset')
# Start the counter.
self.update_clock()
app.repeat(10, self.update_clock)
def update_clock(self):
# Here, we update the value of the timer display on the GUI.
self.text.value = '{:.1f}'.format(self.timer.get_time())
# As an alternative, we could do some periodic task in your
# application, e.g.: adding a new object to the GUI animation;
# updating the score value.
def reset(self):
self.timer.reset()
def stop(self):
app.cancel(self.update_clock)
app = App()
TimerApp(app)
app.display()
"""