-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstopwatch.py
More file actions
executable file
·116 lines (87 loc) · 2.69 KB
/
stopwatch.py
File metadata and controls
executable file
·116 lines (87 loc) · 2.69 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/usr/bin/env python
#
# A trivial little stopwatch application. Useful for tracking 'billable time',
# and 'how long have I wasted on ...' sorts of things.
#
# Usage:
# % python stopwatch.py
#
import datetime
import sys
import pygtk
pygtk.require("2.0")
import gtk
import gobject
ONE_SECOND = datetime.timedelta(seconds=1)
class Stopwatch(object):
def __init__(self):
# The actual stopwatch/time content
self.enabled = False
self.elapsed_time = datetime.timedelta()
# Create the actual stopwatch window
self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
self.window.set_resizable(False)
self.window.set_default_size(-1, 50)
self.window.set_border_width(10)
self.window.set_title("stopwatch")
self.window.show()
# Install callbacks
self.window.connect("destroy", self.destroy)
gobject.timeout_add(1000, self.refresh_time)
# Window layout: elapsed time counter on top; buttons beneath
vbox = gtk.VBox(True, 0)
# ... the actual time display ...
self.time_counter = gtk.Label(str(self.elapsed_time))
self.time_counter.set_width_chars(8) # "hh:mm:ss"
vbox.pack_start(self.time_counter, True, True, 0)
self.time_counter.show()
hbox = gtk.HBox(True, 0)
vbox.pack_start(hbox, True, True, 0)
# ... start/stop toggle button ...
self.toggle_button = gtk.Button("Start")
self.toggle_button.connect("clicked", self.toggle_time)
hbox.pack_start(self.toggle_button, True, True, 0)
self.toggle_button.show()
# ... and a reset button
reset_button = gtk.Button("Reset")
reset_button.connect("clicked", self.reset_time)
hbox.pack_start(reset_button, True, True, 0)
reset_button.show()
hbox.show()
vbox.show()
self.window.add(vbox)
return
def destroy(self, widget, data=None):
"""Window deletion callback. Close the application"""
gtk.main_quit()
def refresh_time(self):
"""Update the elapsed time display. Invoked once per second"""
if (self.enabled):
self.elapsed_time += ONE_SECOND
#self.window.set_title("stopwatch %s" % self.elapsed_time)
self.time_counter.set_text(str(self.elapsed_time))
return True
def reset_time(self, widget, data=None):
"""
Reset the elapsed time back to zero. Invoked when the reset
button is pressed.
"""
self.elapsed_time = datetime.timedelta()
self.time_counter.set_text(str(self.elapsed_time))
return
def toggle_time(self, widget, data=None):
"""
Start or stop the stopwatch, as appropriate. Invoked when the
start/stop button is pressed.
"""
if (self.enabled):
self.enabled = False
self.toggle_button.set_label("Start")
else:
self.enabled = True
self.toggle_button.set_label("Stop")
return
if __name__ == "__main__":
stopwatch = Stopwatch()
gtk.main()
sys.exit(0)