-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
92 lines (70 loc) · 2.88 KB
/
main.py
File metadata and controls
92 lines (70 loc) · 2.88 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
import csv
class Stopwatch(tk.Tk):
def __init__(self):
super().__init__()
self.title("Stopwatch Lap Timer")
self.geometry("300x300")
self.time_var = tk.StringVar()
self.time_var.set("00:00:00")
self.lap_times = []
self.lap_var = tk.StringVar()
self.lap_var.set("Question times: ")
self.create_widgets()
self.is_running = False
self.start_time = None
self.lap_counter = 1
def create_widgets(self):
self.timer_label = tk.Label(self, textvariable=self.time_var, font=("Helvetica", 24))
self.timer_label.pack(pady=20)
self.start_stop_button = tk.Button(self, text="Start", command=self.start_stop)
self.start_stop_button.pack(side=tk.LEFT, padx=10)
self.lap_button = tk.Button(self, text="Lap", command=self.record_lap)
self.lap_button.pack(side=tk.LEFT, padx=10)
self.lap_label = tk.Label(self, textvariable=self.lap_var)
self.lap_label.pack(pady=10)
self.save_laps_button = tk.Button(self, text="Save", command=self.save_laps)
self.save_laps_button.pack(side=tk.BOTTOM, pady=10)
self.paper_name = tk.Text(self, width=20)
self.paper_name.pack(side=tk.BOTTOM)
def start_stop(self):
if not self.is_running:
self.is_running = True
self.start_time = time.time()
self.update_timer()
self.start_stop_button.config(text="Stop")
self.lap_button.config(state=tk.NORMAL)
else:
self.is_running = False
self.start_stop_button.config(text="Start")
self.lap_button.config(state=tk.DISABLED)
def update_timer(self):
if self.is_running:
elapsed_time = time.time() - self.start_time
time_str = self.format_time(elapsed_time)
self.time_var.set(time_str)
self.after(50, self.update_timer)
def format_time(self, elapsed):
minutes = int(elapsed // 60)
seconds = int(elapsed % 60)
milliseconds = int((elapsed % 1) * 1000)
return f"{minutes:02d}:{seconds:02d}:{milliseconds:03d}"
def record_lap(self):
lap_time = self.time_var.get()
self.lap_times.append([self.lap_counter, lap_time])
self.update_lap_label()
self.lap_counter += 1
def update_lap_label(self):
laps_text = "Question times: \n" + f"\n ".join([f"{lap[0]}: {lap[1]}" for lap in self.lap_times])
self.lap_var.set(laps_text)
def save_laps(self):
with open("lap_times.csv", "a", newline='') as f:
writer = csv.writer(f)
paper = self.paper_name.get("1.0", tk.END)
writer.writerow([paper])
for lap in self.lap_times:
writer.writerow(lap)
if __name__ == "__main__":
app = Stopwatch()
app.mainloop()