-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstopwatch.py
More file actions
92 lines (78 loc) · 3.25 KB
/
Copy pathstopwatch.py
File metadata and controls
92 lines (78 loc) · 3.25 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
import tkinter as tk
import time
class StopWatch(tk.Frame):
"""Implements a stopwatch as a tkinter Frame widget."""
def __init__(self, parent=None, **kwargs):
try:
super().__init__(parent, **kwargs)
self._start = 0.0 # Store the start time
self._elapsedtime = 0.0 # Store the elapsed time
self._running = False # Track whether the stopwatch is running
self._timer = None # ID for the after event
self.timestr = tk.StringVar() # StringVar to update label text
self._create_widgets()
except Exception as e:
print(f"Error initializing StopWatch: {e}")
def _create_widgets(self):
"""Create and pack the label widget to display time."""
try:
self._set_time(self._elapsedtime)
label = tk.Label(self, textvariable=self.timestr, font=('Helvetica', 24))
label.pack(fill=tk.X, expand=False, pady=5, padx=5)
except Exception as e:
print(f"Error creating widgets: {e}")
def _update(self):
"""Update the time display every 50 ms."""
try:
self._elapsedtime = time.time() - self._start
self._set_time(self._elapsedtime)
self._timer = self.after(50, self._update)
except Exception as e:
print(f"Error updating time: {e}")
def _set_time(self, elap):
"""Format the time and set it to the label (MM:SS:HS)."""
try:
minutes = int(elap // 60)
seconds = int(elap % 60)
hseconds = int((elap - int(elap)) * 100)
self.timestr.set(f'{minutes:02d}:{seconds:02d}:{hseconds:02d}')
except Exception as e:
print(f"Error formatting time: {e}")
def start(self):
"""Start the stopwatch if it isn't already running."""
if not self._running:
self._start = time.time() - self._elapsedtime
self._update()
self._running = True
def stop(self):
"""Stop the stopwatch if it is currently running."""
if self._running:
if self._timer:
self.after_cancel(self._timer)
self._elapsedtime = time.time() - self._start
self._set_time(self._elapsedtime)
self._running = False
def reset(self):
"""Reset the stopwatch to zero."""
if self._running:
self.stop()
self._elapsedtime = 0.0
self._set_time(self._elapsedtime)
def main():
try:
root = tk.Tk()
root.title("Stopwatch")
stopwatch = StopWatch(root)
stopwatch.pack(side=tk.TOP)
# Control Buttons
btn_frame = tk.Frame(root)
btn_frame.pack(side=tk.TOP, pady=10)
tk.Button(btn_frame, text='Start', command=stopwatch.start).pack(side=tk.LEFT, padx=5)
tk.Button(btn_frame, text='Stop', command=stopwatch.stop).pack(side=tk.LEFT, padx=5)
tk.Button(btn_frame, text='Reset', command=stopwatch.reset).pack(side=tk.LEFT, padx=5)
tk.Button(btn_frame, text='Quit', command=root.destroy).pack(side=tk.LEFT, padx=5)
root.mainloop()
except Exception as e:
print(f"Error in main: {e}")
if __name__ == '__main__':
main()