How to Create Countdown Timer using Python Tkinter (Step by Step)

In this Python Tkinter tutorial, we will discuss how to create a countdown timer using Python Tkinter step by step. Here, we will create a simple GUI-based countdown timer using Python Tkinter.

Create 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 by the user using Python Tkinter. It is not mandatory to provide all the information but should have zero in-place. When a user clicks on the Start Button, the counter should start counting in decreasing order.

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 used imported three modules in this code
    • time : time module is used to perform sleep operation
    • tkinter module is used to create all graphical user interface
    • 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 width
    • 250 is 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 it appears on 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")
  • StringVar() is used to create textvariable, When the text is expected to change multiple times in a program in that case text variable is used.
  • hour.set(“00”) here we have provided the default value to textvariable. So when the program will be executed it will show 00 in place of 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 different textvariable.
  • 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 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
  • startCountdown() function is the most important piece of code in this program as this holds the commands to executive 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 other 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 have accumulated hours, minutes & seconds together now we are separating them.
  • After the countdown information is displayed
  • the counting is happening in a decreasing order every second 1 is reduced from the total seconds & it happens until the total seconds become less than 0.
  • While loop condition will become false & program will be executed with the prompt ‘Times’s up’.

Complete Source Code:

Here is the complete source code to create countdown timer using Python Tkinter and also you can 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 zeros then it will display Time’s up message prompt.

python tkinter countdown timer
python tkinter countdown timer

You may like the following Python Tkinter tutorials:

In this tutorial, we have learned how to create countdown timer using Python Tkinter.