-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcountdown-timer.py
More file actions
115 lines (93 loc) · 4.13 KB
/
Copy pathcountdown-timer.py
File metadata and controls
115 lines (93 loc) · 4.13 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
from threading import Timer
import tkinter as tk
from tkinter import *
from datetime import datetime, time
import winsound
from win10toast import ToastNotifier
# Global list to store timer history
timer_history = []
def countdown(t=None):
if t is None: # Initial call from the button
try:
# If an entry is empty, treat it as 0.
h = int(hour.get() or 0)
m = int(minute.get() or 0)
s = int(second.get() or 0)
t = h * 3600 + m * 60 + s
if t > 0:
# Add to history only if a valid time is set
history_entry = f"{h:02d}:{m:02d}:{s:02d} - "
f"{datetime.now().strftime('%Y-%m-%d %H:%M:%S')}"
timer_history.append(history_entry)
except ValueError:
# If the user enters non-numeric text, show an error.
display_label.config(text="Invalid Input!")
return
if t >= 0:
mins, secs = divmod(t, 60)
hrs, mins = divmod(mins, 60)
display = ("{:02d}:{:02d}:{:02d}".format(hrs, mins, secs))
display_label.config(text=display)
window.after(1000, countdown, t - 1) # Schedule next update
else:
# Timer finished
display_label.config(text="Time's up!!", font=("Helvetica", 45))
display_label.after(3000, lambda: display_label.config(text="00:00:00", font=("Helvetica", 48)))
# Display notification on desktop
toast = ToastNotifier()
toast.show_toast("Notification", "Timer is Off", duration=20, icon_path=None, threaded=True)
# Adding Beep Sound
if check.get():
winsound.Beep(440, 1100)
def update_clock():
"""Updates the clock label with the current time every second."""
now = datetime.now()
current_time = now.strftime("%H:%M:%S")
clock_label.config(text=current_time)
clock_label.after(1000, update_clock)
def show_history():
"""Creates and shows a new window with the timer history."""
history_window = Toplevel(window)
history_window.title("Timer History")
history_window.geometry("300x400")
Label(history_window, text="Input History", font=("bold", 16)).pack(pady=10)
history_listbox = Listbox(history_window, font=("calibri", 12), width=35, height=20)
history_listbox.pack(padx=10, pady=5)
# Populate the listbox with history
for item in timer_history:
history_listbox.insert(END, item)
# Main window setup
window = tk.Tk()
window.geometry("290x450")
window.title("Countdown Timer")
head = Label(window, text="Countdown Timer", font=("bold", 25))
head.pack(pady=20)
# Time variables
hour = StringVar()
minute = StringVar()
second = StringVar()
# Input frame setup
input_frame = Frame(window)
input_frame.pack(pady=10)
Label(input_frame, text="Set Time", font=("bold", 15)).grid(row=0, columnspan=2, pady=10)
Label(input_frame, text="HH", font=("bold", 12)).grid(row=1, column=0, padx=5, pady=2)
Entry(input_frame, textvariable=hour, width=10, font=("bold", 12), justify='center').grid(row=1, column=1, padx=5, pady=2)
Label(input_frame, text="MM", font=("bold", 12)).grid(row=2, column=0, padx=5, pady=2)
Entry(input_frame, textvariable=minute, width=10, font=("bold", 12), justify='center').grid(row=2, column=1, padx=5, pady=2)
Label(input_frame, text="SS", font=("bold", 12)).grid(row=3, column=0, padx=5, pady=2)
Entry(input_frame, textvariable=second, width=10, font=("bold", 12), justify='center').grid(row=3, column=1, padx=5, pady=2)
display_label = Label(window, text="00:00:00", font=("Helvetica", 48))
display_label.pack(pady=20)
check = BooleanVar()
Checkbutton(text="Check for music", onvalue=True, variable = check).pack()
Button(window, text="Set Countdown", command=countdown, bg="yellow").pack(pady=5)
button_frame = Frame(window)
button_frame.pack(pady=5)
# Button(button_frame, text="Set Countdown", command=countdown, bg="yellow").pack(side=LEFT, padx=5)
Button(button_frame, text="History", command=show_history).pack(side=LEFT, padx=5)
#print current time
clock_label = Label(window, font=("calibri", 14, "bold"))
clock_label.pack(pady=10)
update_clock() # Start the clock update loop
window.update()
window.mainloop()