How to Create Countdown Timer using Python Tkinter

In this Python Tkinter tutorial, I discussed how to create a countdown timer using Python Tkinter step by step. We’ll build a simple GUI-based timer that counts down from a given number of seconds. This project helps you learn how to handle time-based events in Tkinter using the after() method.

Create a Countdown Timer using Python Tkinter

Now, let us see how to create a countdown timer using Python Tkinter.

Requirement Analysis

Create a countdown timer that accepts Hours, Minutes & Seconds from the user using Python Tkinter. It is not mandatory to provide all the information, but it should have zero in place. When a user clicks on the Start Button, the counter should start counting in decreasing order.

Read Create Window Titles in Python Tkinter

Source Code Explanation

Here is the explanation of the code to create a countdown timer in Python using Tkinter. Please leave a comment in case you didn’t understand anything or if it didn’t work for you. We have coded this on the Ubuntu operating system, but this code will work on Windows & Macintosh.

import time
from tkinter import *
from tkinter import messagebox
  • Modules used: We have imported three modules in this code
    • time: The time module is used to perform a sleep operation
    • tkinter: module is used to create all graphical user interfaces
    • messagebox: It is used to display prompted messages.
ws = Tk()
ws.geometry("300x250+1500+700")
ws.title("PythonGuides")
ws.config(bg='#345')
  • Creating or initializing window. ws = Tk()
  • geometry is used to set the height, width & position of the application
    • 300 is the width
    • 250 is the height
    • 1500 is the x position on the screen
    • 700 is the y position on the screen
  • title is used to provide the name of the application, which appears at the top of the window.
  • ws.config is used to set the background color of the application
hour=StringVar()
minute=StringVar()
second=StringVar()

hour.set("00")
minute.set("00")
second.set("10")

Check out Create an OptionMenu in Python Tkinter

  • StringVar() is used to create textvariable, When the text is expected to change multiple times in a program in a text variable is used.
  • hour.set(“00”) here we have provided the default value to the textvariable. So when the program is executed, it will show 00 in place of the label.
hour_tf= Entry(
	ws, 
	width=3, 
	font=f,
	textvariable=hour
	)

hour_tf.place(x=80,y=20)

mins_tf= Entry(
	ws, 
	width=3, 
	font=f,
	textvariable=minute)

mins_tf.place(x=130,y=20)

sec_tf = Entry(
	ws, 
	width=3, 
	font=f,
	textvariable=second)

sec_tf .place(x=180,y=20)
  • Three entry boxes are created for hours, minutes & seconds. Each entry box has a width of 3 and a font size of 24 with a different text variable.
  • The place geometry manager is used to position the widgets.
start_btn = Button(
	ws, 
	text='START', 
	bd='5',
	command= startCountdown
	)

start_btn.place(x = 120,y = 120)
  • One button is created with the name ‘START’. This button holds the command that will trigger the countdown activity.
  • pack geometry manager is used to position it on the screen.
def startCountdown():
	try:
		userinput = int(hour.get())*3600 + int(minute.get())*60 + int(second.get())
	except:
		messagebox.showwarning('', 'Invalid Input!')
	while userinput >-1:
		mins,secs = divmod(userinput,60) 

		hours=0
		if mins >60:
			
			hours, mins = divmod(mins, 60)
	
		hour.set("{0:2d}".format(hours))
		minute.set("{0:2d}".format(mins))
		second.set("{0:2d}".format(secs))
	
		ws.update()
		time.sleep(1)

		if (userinput == 0):
			messagebox.showinfo("", "Time's Up")

		userinput -= 1

Read Create Layouts with Python Tkinter Frame

  • startCountdown() function is the most important piece of code in this program, as this holds the commands to execute the code as per the requirement.
  • userinput variable holds the information provided by the user in the interface .get() method pulls the information provided in the entry box
  • 1 hour has 3600 seconds, and 1 minute has 60 seconds. So if a user has entered 2 hours, that means it becomes 7200 seconds similar case is with the minutes.
  • So we have converted hours and minutes into seconds and added all of them together to get total seconds.
  • Also, we have kept userinput in a try-catch block so that in case the user has provided input other than numbers, then an error prompt will appear.
  • while loop is started & will keep on looping until the total seconds are less than 0 or -1.
  • inside loop divmod() function is used. divmode is similar to ‘//‘ operator. It returns the integer values only after performing division.
  • Earlier, we had accumulated hours, minutes & seconds together; now we are separating them.
  • After the countdown information is displayed,
  • the countdown is happening in a decreasing order every second, 1 is reduced from the total seconds & it continues until the total seconds become less than 0.
  • While loop condition will become false & the program will be executed with the prompt ‘Times’s up’.

Complete Source Code:

Here is the complete source code to create a countdown timer using Python Tkinter, and you can also see the output.

import time
from tkinter import *
from tkinter import messagebox

f = ("Arial",24)

ws = Tk()
ws.geometry("300x250+1500+700")
ws.title("PythonGuides")
ws.config(bg='#345')

hour=StringVar()
minute=StringVar()
second=StringVar()

hour.set("00")
minute.set("00")
second.set("10")

hour_tf= Entry(
	ws, 
	width=3, 
	font=f,
	textvariable=hour
	)

hour_tf.place(x=80,y=20)

mins_tf= Entry(
	ws, 
	width=3, 
	font=f,
	textvariable=minute)

mins_tf.place(x=130,y=20)

sec_tf = Entry(
	ws, 
	width=3, 
	font=f,
	textvariable=second)

sec_tf.place(x=180,y=20)

def startCountdown():
	try:
		userinput = int(hour.get())*3600 + int(minute.get())*60 + int(second.get())
	except:
		messagebox.showwarning('', 'Invalid Input!')
	while userinput >-1:
		mins,secs = divmod(userinput,60) 

		hours=0
		if mins >60:
			hours, mins = divmod(mins, 60)
	
		hour.set("{0:2d}".format(hours))
		minute.set("{0:2d}".format(mins))
		second.set("{0:2d}".format(secs))
	
		ws.update()
		time.sleep(1)
	
		if (userinput == 0):
			messagebox.showinfo("", "Time's Up")

		userinput -= 1
start_btn = Button(
	ws, 
	text='START', 
	bd='5',
	command= startCountdown
	)

start_btn.place(x = 120,y = 120)

ws.mainloop()

Output:

Here is the output of the above code. Users can edit the numbers appearing on the screen. The start button will initiate the countdown, and once the countdown reaches back to zero then it will display Time’s up message prompt.

python tkinter countdown timer

In this tutorial, I helped you learn how to create a countdown timer using Python Tkinter. I also explained the part of the code in brief.

You may like the following Python Tkinter tutorials:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.