-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcountdown timer.py
67 lines (51 loc) · 1.67 KB
/
countdown timer.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
import time
from tkinter import *
from tkinter import messagebox
# Create Tk window
root = Tk()
# Set geometry of the window
root.geometry("300x250")
# Title for the window
root.title("Time Counter")
# Declare variables for the time inputs
hour = StringVar()
minute = StringVar()
second = StringVar()
# Set default values
hour.set("00")
minute.set("00")
second.set("00")
# Entry widgets for user input
hourEntry = Entry(root, width=3, font=("Arial", 18, ""), textvariable=hour)
hourEntry.place(x=80, y=20)
minuteEntry = Entry(root, width=3, font=("Arial", 18, ""), textvariable=minute)
minuteEntry.place(x=130, y=20)
secondEntry = Entry(root, width=3, font=("Arial", 18, ""), textvariable=second)
secondEntry.place(x=180, y=20)
def submit():
try:
# Convert input to total seconds
total_seconds = int(hour.get()) * 3600 + int(minute.get()) * 60 + int(second.get())
except ValueError:
messagebox.showerror("Invalid Input", "Please enter valid numbers")
return
while total_seconds > -1:
# Calculate the time components
mins, secs = divmod(total_seconds, 60)
hours, mins = divmod(mins, 60)
# Format and update the variables
hour.set(f"{hours:02d}")
minute.set(f"{mins:02d}")
second.set(f"{secs:02d}")
# Update the window
root.update()
time.sleep(1)
if total_seconds == 0:
messagebox.showinfo("Time Countdown", "Time's up")
# Decrement the total time
total_seconds -= 1
# Button to start the countdown
btn = Button(root, text='Start Countdown', bd='5', command=submit)
btn.place(x=90, y=120)
# Run the Tkinter event loop
root.mainloop()