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
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