Site icon TechVidvan

Create Countdown Timer using Python

python countdown timer

Python Countdown Timer – Python project for beginners, is used to set a countdown timer. It displays how much time is left in your set time. It is like an alarm that will give the user options to set the time and when that time will over it will notify the user.

Countdown Clock & Timer Python Project

The objective of this python project is to create a countdown timer and display time. In this python project, the user sets the time and by click on the start button, it starts the count from that time. When the time gets over ie when the countdown timer finishes it plays a sound.

This project build with basic concept of python and libraries: Tkinter, time and playsound

Steps to Develop Python Countdown Timer

Project Prerequisites

Tkinter is a standard GUI Python library from which we can build GUI applications in the fastest and easiest ways.

The playsound module is used when we want to play audio files with a single line of code. This doesn’t have any dependencies.

The Python time module provides ways to represent time.

To install the library, please run pip install command in prompt:

pip install tkinter                                                                                                                                                                                     
pip install time
pip install playsound

Download Countdown Timer Python Code

Please download source code of Countdown Timer Project: Python Countdown Timer

These are the steps to builds Countdown Clock And Timer Python project :

1. Importing Required Module

from tkinter import *
import time
from playsound import playsound

The first step in the program is to import libraries. Here, we required three modules so we need to import tkinter, time and playsound modules.

2. Create a Display Window

root = Tk()
root.geometry('400x300')
root.resizable(0,0)
root.config(bg ='blanched almond')
root.title('TechVidvan - Countdown Clock And Timer')
Label(root, text = 'Countdown Clock and Timer' , font = 'arial 20 bold',  bg ='papaya whip').pack()

3. Create Current Time

Label(root, font ='arial 15 bold', text = 'current time :', bg = 'papaya whip').place(x = 40 ,y = 70)

def clock():
    clock_time = time.strftime('%H:%M:%S %p')
    curr_time.config(text = clock_time)
    curr_time.after(1000,clock)

curr_time =Label(root, font ='arial 15 bold', text = '', fg = 'gray25' ,bg ='papaya whip')
curr_time.place(x = 190 , y = 70)
clock()

4. Create Function to Start Timer

sec = StringVar()
Entry(root, textvariable = sec, width = 2, font = 'arial 12').place(x=250, y=155)
sec.set('00')

mins= StringVar()
Entry(root, textvariable = mins, width =2, font = 'arial 12').place(x=225, y=155)
mins.set('00')

hrs= StringVar()
Entry(root, textvariable = hrs, width =2, font = 'arial 12').place(x=200, y=155)
hrs.set('00’)

Entry() widget is used to create an input text field.

def countdown():
    times = int(hrs.get())*3600+ int(mins.get())*60 + int(sec.get())
    while times > -1:
        minute,second = (times // 60 , times % 60)
        
        hour = 0
        if minute > 60:
            hour , minute = (minute // 60 , minute % 60)
      
        sec.set(second)
        mins.set(minute)
        hrs.set(hour)
   
        root.update()
        time.sleep(1)

        if(times == 0):
            playsound('Loud_Alarm_Clock_Buzzer.mp3')
            sec.set('00')
            mins.set('00')
            hrs.set('00')
        times -= 1

5. Create Button

Label(root, font ='arial 15 bold', text = 'set the time',   bg ='papaya whip').place(x = 40 ,y = 150)

Button(root, text='START', bd ='5', command = countdown, bg = 'antique white', font = 'arial 10 bold').place(x=150, y=210)
 
root.mainloop()

Button() widget used to display a button on window

root.mainloop() is a method which executes when we want to run our program

Python Countdown Timer Project Output

Summary

With this project in Python, we have successfully created the countdown clock and timer python project. We used the python tkinter library for graphics, time library to display current time, and playsound library to play sound. We learned how to display the current time and how to build a countdown timer.

Exit mobile version