Debian Codegraph @

examples/timer.py

raw symbols SCIP
1 #!/usr/bin/env python
2 """A full-screen minute:second timer. Leave it in charge of your conference
3 lighting talks.
4
5 After 5 minutes, the timer goes red. This limit is easily adjustable by
6 hacking the source code.
7
8 Press spacebar to start, stop and reset the timer.
9 """
10
11 import pyglet
12
13 window = pyglet.window.Window(fullscreen=True)
14
15
16 class Timer:
17 def __init__(self):
18 self.label = pyglet.text.Label('00:00', font_size=360,
19 x=window.width//2, y=window.height//2,
20 anchor_x='center', anchor_y='center')
21 self.reset()
22
23 def reset(self):
24 self.time = 0
25 self.running = False
26 self.label.text = '00:00'
27 self.label.color = (255, 255, 255, 255)
28
29 def update(self, dt):
30 if self.running:
31 self.time += dt
32 m, s = divmod(self.time, 60)
33 self.label.text = f'{round(m):02}:{round(s):02}'
34 if m >= 5:
35 self.label.color = (180, 0, 0, 255)
36
37 window.draw(dt=dt)
38
39
40 @window.event
41 def on_key_press(symbol, modifiers):
42 if symbol == pyglet.window.key.SPACE:
43 if timer.running:
44 timer.running = False
45 else:
46 if timer.time > 0:
47 timer.reset()
48 else:
49 timer.running = True
50 elif symbol == pyglet.window.key.ESCAPE:
51 window.close()
52
53
54 @window.event
55 def on_draw():
56 window.clear()
57 timer.label.draw()
58
59
60 timer = Timer()
61
62 # Set timer and redraw update rate to 30 FPS
63 pyglet.clock.schedule_interval(timer.update, 1/30.0)
64
65 # Launch the application
66 pyglet.app.run(None)
67