In today’s tutorial, we are going to create a countdown timer where the user can input hours, minutes, or seconds and then click start, and once the countdown reaches zero an alarm goes off.
Let’s get started!
Table of Contents
- Necessary Libraries
- Imports
- Function
- Creating the Main Window and Label
- Creating Entry Widgets and Labels
- Start Button and Timer Label
- Main Loop
- Example
- Full Code
Necessary Libraries
For this code to function properly, make sure to install these libraries:
$ pip install tk
$ pip install playsound
Imports
from tkinter import *
from playsound import playsound
import time
We start by importing the tkinter library, which allows us to create a graphical user interface (GUI). Then we import the playsound library which is used to play audio files.
After that, we import the time
module so we can use time-related functions.
Function
def start_timer():
try:
hours = int(entry_hours.get())
minutes = int(entry_minutes.get())
seconds = int(entry_seconds.get())
except ValueError:
return
total_seconds = hours * 3600 + minutes * 60 + seconds
for t in range(total_seconds, -1, -1):
mins, secs = divmod(t, 60)
hours, mins = divmod(mins, 60)
time_str = f"{hours:02d}:{mins:02d}:{secs:02d}"
timer_label.config(text=time_str)
root.update()
time.sleep(1)
# Time's up!
timer_label.config(text="Time's up!")
playsound("your_alarm_sound.mp3") # Replace with your sound file path
Next, we create a function named start_timer
that initiates the countdown by converting the user’s input (desired time) into integers. However, if the user inputs a non-numerical value, an error appears. Once the error is corrected the function calculates the total time inputted by the user in seconds. Then it runs a countdown loop, updating the timer label with the formatted time at each iteration.
After the countdown reaches zero, it updates the timer label to indicate that the time is up and plays an alarm sound using the playsound
library.
Creating the Main Window and Label
root = Tk()
root.title("Countdown Timer - The Pycodes")
root.geometry("500x400")
root.config(bg="black")
Label(root, text="Countdown", font="arial 20 bold", bg="black", fg="orange").place(x=180, y=20)
Now, we create the main window, give it a title, and define its geometry and background color, as well as create a label that says ‘Countdown‘ with a specific font, size, color, and coordinates.
Creating Entry Widgets and Labels
entry_hours = Entry(root, width=5, font=("Arial", 20), bg="black", fg="#fff", bd=0)
entry_hours.place(x=100, y=100)
entry_hours.insert(3, "00")
label_hours = Label(root, text="Hours", font=("Arial", 15), bg="black", fg="#fff")
label_hours.place(x=30, y=100)
entry_minutes = Entry(root, width=5, font=("Arial", 20), bg="black", fg="#fff", bd=0)
entry_minutes.place(x=270, y=100)
entry_minutes.insert(0, "00")
label_minutes = Label(root, text="Minutes", font=("Arial", 15), bg="black", fg="#fff")
label_minutes.place(x=180, y=100)
entry_seconds = Entry(root, width=5, font=("Arial", 20), bg="black", fg="#fff", bd=0)
entry_seconds.place(x=450, y=100)
entry_seconds.insert(0, "00")
label_seconds = Label(root, text="Seconds", font=("Arial", 15), bg="black", fg="#fff")
label_seconds.place(x=350, y=100)
Here, we created Entry widgets where the user can input his desired value for the countdown timer whether it’s hours, minutes, or seconds, and give them the initial value of 00
, and then create labels that say ‘Hours‘, ‘Minutes‘, and ‘Seconds‘ as a sign of what each entry widget is.
Start Button and Timer Label
start_button = Button(root, text="Start", command=start_timer, font=("Arial", 15))
start_button.place(x=225, y=250)
timer_label = Label(root, text="00:00:00", font=("Arial", 40), bg="black", fg="orange", bd=0)
timer_label.place(x=150, y=180)
Following that, we create a “Start” button once clicked triggers the start_timer
function as well as the timer label that will display the countdown.
Main Loop
root.mainloop()
Lastly, this part makes sure that the main window keeps running and is interactive with the user until he chooses to exit.
Example
Full Code
from tkinter import *
from playsound import playsound
import time
def start_timer():
try:
hours = int(entry_hours.get())
minutes = int(entry_minutes.get())
seconds = int(entry_seconds.get())
except ValueError:
return
total_seconds = hours * 3600 + minutes * 60 + seconds
for t in range(total_seconds, -1, -1):
mins, secs = divmod(t, 60)
hours, mins = divmod(mins, 60)
time_str = f"{hours:02d}:{mins:02d}:{secs:02d}"
timer_label.config(text=time_str)
root.update()
time.sleep(1)
# Time's up!
timer_label.config(text="Time's up!")
playsound("your_alarm_sound.mp3") # Replace with your sound file path
root = Tk()
root.title("Countdown Timer - The Pycodes")
root.geometry("500x400")
root.config(bg="black")
Label(root, text="Countdown", font="arial 20 bold", bg="black", fg="orange").place(x=180, y=20)
entry_hours = Entry(root, width=5, font=("Arial", 20), bg="black", fg="#fff", bd=0)
entry_hours.place(x=100, y=100)
entry_hours.insert(3, "00")
label_hours = Label(root, text="Hours", font=("Arial", 15), bg="black", fg="#fff")
label_hours.place(x=30, y=100)
entry_minutes = Entry(root, width=5, font=("Arial", 20), bg="black", fg="#fff", bd=0)
entry_minutes.place(x=270, y=100)
entry_minutes.insert(0, "00")
label_minutes = Label(root, text="Minutes", font=("Arial", 15), bg="black", fg="#fff")
label_minutes.place(x=180, y=100)
entry_seconds = Entry(root, width=5, font=("Arial", 20), bg="black", fg="#fff", bd=0)
entry_seconds.place(x=450, y=100)
entry_seconds.insert(0, "00")
label_seconds = Label(root, text="Seconds", font=("Arial", 15), bg="black", fg="#fff")
label_seconds.place(x=350, y=100)
start_button = Button(root, text="Start", command=start_timer, font=("Arial", 15))
start_button.place(x=225, y=250)
timer_label = Label(root, text="00:00:00", font=("Arial", 40), bg="black", fg="orange", bd=0)
timer_label.place(x=150, y=180)
root.mainloop()
Happy Coding!