-
Notifications
You must be signed in to change notification settings - Fork 0
/
stopwatch.py
executable file
·102 lines (85 loc) · 3.24 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
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
#!/usr/bin/env python
import os
from threading import Thread
import time, datetime
import Tkinter
class Stopwatch:
"""Generic stopwatch class. Controls starting and keeping track of
the time."""
def __init__(self):
"""Initialize Stopwatch instance."""
self.first_time = 0.
self.curr_time = 0.
self.running = False
self.thread = None
def start(self, string_var):
"""Spawn a new thread to start the stopwatch."""
self.running = True
self.first_time = datetime.datetime.now()
self.thread = StopwatchThread(self, string_var)
self.thread.start()
def stop(self, string_var):
"""Stop the subthread to stop the stopwatch."""
self.running = False
self.thread._Thread__stop()
self.first_time = 0.
def toggle(self, string_var):
"""Toggle state of stopwatch."""
if self.running:
self.stop(string_var)
else:
self.start(string_var)
class StopwatchThread(Thread):
"""Thread for stopwatch counting."""
def __init__(self, watch, string_var):
"""Initialize the StopwatchThread instance."""
Thread.__init__(self)
self.watch = watch
self.string_var = string_var
def run(self):
"""Main thread method."""
while True:
self.watch.curr_time = datetime.datetime.now()
the_time = self.watch.curr_time - self.watch.first_time
#self.string_var.set(time.strftime("%H:%M:%S", time.gmtime(the_time)))
hours = the_time.seconds/(60*60)
minutes = the_time.seconds/60
seconds = the_time.seconds%60
self.string_var.set("%02i:%02i:%02i.%02i" % (hours, minutes, seconds, the_time.microseconds/1e4))
time.sleep(0.005)
if not self.isAlive():
break
class TkStopwatch:
"""Class for Tk version of the stopwatch."""
def __init__(self, parent, stopwatch):
"""Initialize and pack all relevant widgets."""
self.parent = parent
self.stopwatch = stopwatch
self.time_string = Tkinter.StringVar(value="00:00:00.00")
self.startstop_string = Tkinter.StringVar(value="Start")
# Current time
self.lTime = Tkinter.Label(self.parent, textvariable=self.time_string, font=("FreeSans", 20))
self.lTime.pack(side=Tkinter.LEFT)
# Starts/stops the stopwatch
self.bStartStop = Tkinter.Button(self.parent, textvariable=self.startstop_string, command=self.toggle)
self.bStartStop.pack(side=Tkinter.LEFT)
# Quit button
self.bQuit = Tkinter.Button(self.parent, text="Quit", fg="red", command=self.quit)
self.bQuit.pack(side=Tkinter.LEFT)
def toggle(self):
"""Toggle stopwatch state."""
if self.stopwatch.running:
self.startstop_string.set("Start")
else:
self.startstop_string.set("Stop")
self.stopwatch.toggle(self.time_string)
def quit(self):
"""Quit program."""
self.stopwatch.stop(self.time_string)
self.parent.quit()
if __name__ == "__main__":
watch = Stopwatch()
tkroot = Tkinter.Tk()
tkroot.title("Stopwatch")
tkwatch = TkStopwatch(tkroot, watch)
tkroot.mainloop()