-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
73 lines (54 loc) · 2.3 KB
/
main.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
import threading
import time
import tkinter as tk
from win10toast import ToastNotifier
class CountdownTimer:
def __init__(self):
self.root = tk.Tk()
self.root.title("CountdownTimer")
self.root.resizable(False, False)
self.root.geometry("460x220")
self.time_entry = tk.Entry(self.root, font=("Helvetica", 30))
self.time_entry.grid(row=0, column=0, columnspan=2, padx=5, pady=5)
self.start_btn = tk.Button(self.root, font=("Helvetica", 30), text="Start", command=self.start_thread)
self.start_btn.grid(row=1, column=0, padx=5, pady=5)
self.stop_btn = tk.Button(self.root, font=("Helvetica", 30), text="Stop", command=self.stop)
self.stop_btn.grid(row=1, column=1, padx=5, pady=5)
self.time_label = tk.Label(self.root, font=("Helvetica", 30), text="Time: 00:00:00")
self.time_label.grid(row=2, column=0, columnspan=2, pady=5, padx=5)
self.stop_loop = False
self.root.mainloop()
def start_thread(self):
t = threading.Thread(target=self.start)
t.start()
def start(self):
self.stop_loop = False
hours, minutes, seconds = 0, 0, 0
string_split = self.time_entry.get().split(":")
if len(string_split) == 3:
hours = int(string_split[0])
minutes = int(string_split[1])
seconds = int(string_split[2])
elif len(string_split) == 2:
minutes = int(string_split[0])
seconds = int(string_split[1])
elif len(string_split) == 1:
seconds = int(string_split[0])
else:
print("Invalid Time Format")
return
full_seconds = hours * 3600 + minutes * 60 + seconds
while full_seconds > 0 and not self.stop_loop:
full_seconds -= 1
minutes, seconds = divmod(full_seconds, 60)
hours, minutes = divmod(minutes, 60)
self.time_label.config(text=f"Time: {hours:02d}:{minutes:02d}:{seconds:02d}")
self.root.update()
time.sleep(1)
if not self.stop_loop:
toast = ToastNotifier()
toast.show_toast("Countdown Timer ", "Time is up!", duration=10)
def stop(self):
self.stop_loop = True
self.time_label.config(text="Time: 00:00:00")
CountdownTimer()