-
Notifications
You must be signed in to change notification settings - Fork 0
/
stopwatch.py
executable file
·55 lines (47 loc) · 1.43 KB
/
stopwatch.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
#!/usr/bin/python2
import pyglet
from pyglet.window import key
import time
running = False
begin, now, total = 0, 0, 0
size = (320, 240)
window = pyglet.window.Window(size[0], size[1], caption="Stopwatch",
visible=False)
label = pyglet.text.Label('00:00:00:00', font_size=36,
x=window.width/2, y=window.height/2,
anchor_x='center', anchor_y='center')
window.set_visible()
@window.event
def on_key_release(symbol, modifiers):
global now, begin, running, total
if symbol == key.SPACE:
if not running:
running = True
begin = time.time()
else:
running = False
now = time.time()
total += now - begin
if symbol == key.BACKSPACE:
total = 0
begin = time.time()
running = False
@window.event
def on_draw():
window.clear()
label.draw()
def get_millis(timestamp):
millis = str(timestamp - int(timestamp))[2:4]
if millis == "":
return "00"
else:
return millis
def update_time(dt):
global now, begin, total
if running:
now = time.time()
label.text = time.strftime("%H:%M:%S:", time.gmtime(now - begin + total)) + get_millis(now - begin + total)
else:
label.text = time.strftime("%H:%M:%S:", time.gmtime(total)) + get_millis(total)
pyglet.clock.schedule_interval(update_time, 0.01)
pyglet.app.run()