Countdown Clock And Timer Python Project
In this tutorial, you'll learn how to use Python Tkinter to create a countdown clock and timer.
A countdown timer is a web page's virtual clock that counts down from a given number or date to signify the start or finish of an event or promotion. The simplest example of a countdown clock and timer using Python Tkinter is shown here.
Using Python Tkinter, make a countdown timer.
We have provided a step-by-step description of the code here.
Import the required modules.
To begin, we must import all of the necessary modules at the top. Three modules were imported by the supplied code: time, tkinter, and messagebox. The time module is used to show time, tkinter is used to generate all graphical user interfaces, and the messagebox module is used to prompt messages.
Tkinter Object Creation
The Tk class was then used to build a top-level object. This generates a window for your complete graphical user interface (GUI) program. The width and height of the application are determined by the geometry.
Creating the time entry box
We've generated three input widgets for hours, minutes, and seconds, each with a default value of '00'.
Developing a start button
Next, we added a button to start the timer and called the countdowntimer method ().
Countdown to the event
We've created a countdowntimer function here (). This is the most significant component of the code since it contains the directives for executing the code as needed. The information given by the user in the interface is stored in the variable user input. Then we determined the total seconds by converting all the hours and minutes to seconds. The user input variable was preserved in the try block to handle the exception if the user gave erroneous data. The while loop repeats until the total seconds are less than or equal to 0 or -1. In each cycle, the number of seconds is lowered by one, until the message 'Time Over' appears.
Source Code for Countdown Clock And Timer Python Project
from tkinter import *
from tkinter import messagebox
# Create object
root = Tk()
# Define the geometry of the window
# set background color
# declaration of variables
# setting the default value as 0
# Using Entry class to take input from the user
mins_box = Entry(
sec_box = Entry(
# store the user input
user_input = int(hour.get())*3600 + int(minute.get())*60 + int(second.get())
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,
if mins >60:
hours, mins = divmod(mins, 60)
# store the value up to two decimal places
# using the format() method
# updating the GUI window
# 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',
btn.place(x = 80,y = 120)