Countdown clock and timer using Tkinter in Python
In this post, you will learn how to create a countdown clock and timer using the Python Tkinter.
Tkinter is a standard cross-platform package for creating graphical user interfaces (GUIs). It is an original GUI library for Tcl (Tool Command Language). Tkinter comes pre-installed with Python. The greatest strength of Tkinter is its simplicity. It comes with a wide range of necessary widgets for almost all common tasks, like text, buttons, radio buttons, checkboxes, scales, labels, or drawing canvas. Tkinter provides the Tk GUI toolkit with a potent object-oriented interface. It is an easy task to create a GUI using Tkinter.
A countdown timer is a virtual clock on a web page that counts down from a certain number or date to indicate the beginning or end of an event or offer. Here, we have mentioned the simplest example of a countdown clock and timer using Python Tkinter.
Create a countdown timer using Python Tkinter
Here, we have mentioned the step-by-step explanation of the code.
Import the necessary modules
First, we need to import all the required modules at the top. The given code imports three modules: time, tkinter, and messagebox. The time module is used for displaying time, the tkinter creates all GUIs and the messagebox module is used to prompt messages.
import time
from tkinter import *
from tkinter import messagebox
Creating Tkinter Object
Next, we have created a top-level object using the Tk class. This creates a window for your entire GUI application. The geometry is used to set the width and height of the application.
# Create object
root = Tk()
# Define the geometry of the window
root.geometry("500x450")
#define title
root.title("Countdown timer")
# set background color
root.config(bg='#345')
Creating the entry box for time
Here, we have created three entry widgets each for hours, minutes, and seconds and set their value to '00' by default.
# setting the default value as 00
hour.set("00")
minute.set("00")
second.set("00")
hour_box= Entry(
root,
width=3,
font=f,
textvariable=hour
)
hour_box.place(x=80,y=20)
mins_box = Entry(
root,
width=3,
font=f,
textvariable=minute)
mins_box.place(x=130,y=20)
sec_box = Entry(
root,
width=3,
font=f,
textvariable=second)
sec_box.place(x=180,y=20)
Creating start button
Next, we have created a button for setting the timer and triggered the function countdowntimer().
# button widget
btn = Button(root, text='Set Time Countdown', bd='5',
command= countdowntimer)
btn.place(x = 80,y = 120)
Countdown event
Here, we have defined a function countdowntimer(). This is the most important part of the code, as this function holds the commands to execute the code as per the requirement. We have defined a variable user_input to hold the information provided by the user in the interface. Then, we converted all the hours and minutes into seconds and calculated the total seconds. We kept the user_input in the try block to handle the exception in case the user provided invalid input. The while loop iterates over the seconds until the total seconds are less than 0 or -1. Every second 1 is reduced in each iteration, and at last it prompts 'Time Over'.
def countdowntimer():
try:
# store the user input
user_input = int(hour.get())*3600 + int(minute.get())*60 + int(second.get())
except:
messagebox.showwarning('', 'Invalid Input!')
while user_input >-1:
# divmod(firstvalue = user_input//60, secondvalue = user_input%60)
mins,secs = divmod(user_input,60)
# Converting the input entered in mins or secs to hours,
hours=0
if mins >60:
hours, mins = divmod(mins, 60)
# store the value up to two decimal places
# using the format() method
hour.set("{0:2d}".format(hours))
minute.set("{0:2d}".format(mins))
second.set("{0:2d}".format(secs))
# updating the GUI window
root.update()
time.sleep(1)
# if user_input value = 0, then a messagebox pop's up
# with a message
if (user_input == 0):
messagebox.showinfo("Time Countdown", "Time Over")
# decresing the value of temp
# after every one sec by one
user_input -= 1
Complete code
Here, we have merged all the above code to get the complete source code to create a countdown clock and timer using Python Tkinter.
import time
from tkinter import *
from tkinter import messagebox
# Create object
root = Tk()
# Define the geometry of the window
root.geometry("400x300")
#define title
root.title("Countdown timer")
# set background color
root.config(bg='#345')
# declaration of variables
hour=StringVar()
minute=StringVar()
second=StringVar()
# setting the default value as 0
hour.set("00")
minute.set("00")
second.set("00")
# Using Entry class to take input from the user
hour_box= Entry(
root,
width=3,
font=("Arial",18,""),
textvariable=hour
)
hour_box.place(x=80,y=20)
mins_box = Entry(
root,
width=3,
font=("Arial",18,""),
textvariable=minute)
mins_box.place(x=130,y=20)
sec_box = Entry(
root,
width=3,
font=("Arial",18,""),
textvariable=second)
sec_box.place(x=180,y=20)
def countdowntimer():
try:
# store the user input
user_input = int(hour.get())*3600 + int(minute.get())*60 + int(second.get())
except:
messagebox.showwarning('', 'Invalid Input!')
while user_input >-1:
# divmod(firstvalue = user_input//60, secondvalue = user_input%60)
mins,secs = divmod(user_input,60)
# Converting the input entered in mins or secs to hours,
hours=0
if mins >60:
hours, mins = divmod(mins, 60)
# store the value up to two decimal places
# using the format() method
hour.set("{0:2d}".format(hours))
minute.set("{0:2d}".format(mins))
second.set("{0:2d}".format(secs))
# updating the GUI window
root.update()
time.sleep(1)
# if user_input value = 0, then a messagebox pop's up
# with a message
if (user_input == 0):
messagebox.showinfo("Time Countdown", "Time Over")
# decresing the value of temp
# after every one sec by one
user_input -= 1
# button widget
btn = Button(root, text='Set Time Countdown', bd='5',
command= countdowntimer)
btn.place(x = 80,y = 120)
root.mainloop()
Output of the above code:
Related Articles
Countdown clock and timer using Tkinter in PythonSimple Calculator Program in Python
Python gmplot to add google map on a web page
Python requests GET method
Python OpenCV ColorMap
Python 3 Tkinter Menu Bar
Python Tkinter Frame Widget
Python Tkinter Checkbutton Widget
Python Tkinter Combobox Event Binding
Python Tkinter Checkbutton Widget
Bouncing ball game code in Python
Python Tkinter Combobox
Python Tkinter Text Widget
Entry Field Validation in Tkinter Python
Python Tkinter Scale Widget
Python Tkinter Overview and Examples
Python3 Tkinter Messagebox
Entry Field Validation in Tkinter Python
Python 3 Tkinter Menu Bar
Add background image in Python Tkinter