Code’s Pulse — Python Countdown Timer - A Simple Project

Himani Bansal
Wiki Flood
Published in
3 min readNov 29, 2023

About Python Countdown Timer

In this Python Project, we will build a Countdown timer application in Python. We build this application using the tkinter library for making a simple GUI; the time module for setting the time in application; the win10toast for displaying the notification.

Prerequisites For Python Countdown Timer

  • First, we’ll initiate the project by installing the essential libraries and modules via the pip installer.
  • A strong grasp of Python, coupled with proficiency in the required libraries and modules, is crucial for the successful development of this project.
Python Countdown Timer Project
Python Countdown Timer Project

pip install tkinter
pip install python-time
pip install threaded
pip install win10toast

Importing the required libraries and modules in program:

from tkinter import *
import time
import threading
from win10toast import ToastNotifier

Creating a class and initializing the GUI window:

class Timer:
def __init__(self):
self.root = Tk()
self.root.geometry("460x200")
self.root.title("PythonFlood-Countdown Timer")
self.root.config(bg='#BCD2EE')


self.time_en = Entry(self.root, font=('Arial', 30))
self.time_en.grid(row=0, column=0, columnspan=2, padx=5, pady=10)


self.btn1 = Button(self.root, text="Start", font=('Arial', 15), bd=5,
bg='#6E7B8B', fg='White', command=self.start_thread)
self.btn1.grid(row=1, column=0, padx=5, pady=5)


self.btn2 = Button(self.root, text="Stop", font=('Arial', 15), bd=5,
bg='#6E7B8B', fg='White', command=self.stop)
self.btn2.grid(row=1, column=1, padx=5, pady=5)


self.lbl = Label(self.root, text="Time: 00:00:00", font=('Arial', 15),
bg='#6E7B8B', fg='White')
self.lbl.grid(row=2, column=0, columnspan=2, padx=5, pady=5)


self.stop_loop = False
self.root.mainloop()

root- It is the name of our GUI window.

Tk()- It initialises tkinter which means a GUI window is created.

geometry()- This method provides the length and breadth to the GUI window.

resizeable()- This method allows the window to change its size as per user need.

title()- This method gives title to the window

confg()- This method sets the configuration of the window.

Entry- In this widget, users will enter the random words of their choice.

Label()- It is used to display one line or more than one line of text.

text- It is used to display text on label.

font- It is a style in which font is written.

bg- It is the background Colour of the label.

Button()- It is a button used to display on our window.

command- It is used as a function of a button when it is clicked.

grid()- It is used to set the position by defining row and column.

root.mainloop()- It is simply a method in the main window that executes what we wish to execute in an application and ends the mainloop.

Code for creating a thread:

def start_thread(self):
t = threading.Thread(target=self.start)
t.start()

This method starts a new thread to run the timer. It’s called when the “Start” button is clicked.

Code for Main function and notification- We will create a main function.

def start(self):
self.stop_loop = False


hours, minutes, seconds = 0, 0, 0
str_split = self.time_en.get().split(":")
if len(str_split) == 3:
hours = int(str_split[0])
minutes = int(str_split[1])
seconds = int(str_split[2])
elif len(str_split) == 2:
minutes = int(str_split[0])
seconds = int(str_split[1])
elif len(str_split) == 1:
seconds = int(str_split[0])
else:
print("Invalid Time Format")
return


full_secs = hours * 3600 + minutes * 60 + seconds


while full_secs > 0 and not self.stop_loop:
full_secs -= 1


minutes, seconds = divmod(full_secs, 60)
hours, minutes = divmod(minutes, 60)


self.lbl.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's Up", duration=10)
  • Parses the user input for the countdown time in hours, minutes, and seconds.
  • Calculate the total time in seconds.
  • Enter a loop that counts down from the total time to zero.
  • Updates the label (self.lbl) to display the remaining time.
  • Checks for valid time input formats.
  • When the countdown reaches zero, it displays a Windows notification using the win10toast library.

Code for stop function- Now we will create a stop function.


def stop(self):
self.stop_loop = True
self.lbl.config(text="Time: ")




Timer()
  • Stop the timer loop by setting self.stop_loop to True.
  • Clears the label text to reset the display.
  • Creates an instance of the Timer() class to start the timer application.

Python Countdown Timer Output

Python Countdown Timer Project Output
Python Countdown Timer Project Output

Conclusion

You’ve successfully created a Countdown Timer application using Python, utilizing tkinter for the graphical user interface and win10toast for displaying notifications. This application allows users to set a countdown time and receive a notification when the timer reaches zero. You can customize the application’s interface to suit your preferences.

--

--

Himani Bansal
Wiki Flood

Doing my Best to Explain Data Science (Data Scientist, technology freak & Blogger)