Create Countdown Timer using Python

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.

Keeping you updated with latest technology trends, Join TechVidvan on Telegram

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 :

  • Import required Modules
  • Create a display window
  • Display current time
  • Create a countdown timer
  • Create a button

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()
  • Tk() initializes tkinter to create the window
  • geometry() set the width and height of the window
  • resizable(0,0) prohibit users to resize the window
  • title() used to set the title of display window
  • bg used to set the color of background
  • Label() widget used to display one or more than one line of text that users can’t modify
  • root is the name of our window
  • text which we display on the label
  • font in which the text is written
  • pack organizes widget in block

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()
  • strftime() method return the string representing the time in given format
  • after() method used to give a delay of 1000 millisecond which is 1 sec

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’)
  • sec is a string type variable that stores the seconds.
  • mins is a string type variable that stores the minutes.
  • hrs is a string type variable that stores the hours

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

  • textvariable used to retrieve text from specific variable to entry widget
  • place() widgets place widgets in a specific position in the parent widget.
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
  • times variable gets the hours value multiplied by 3600 (1 h = 3600 sec), mins value multiplied by 60 (1 min = 60 sec), and sec value.
  • ‘//’ operator returns the result of the division in a whole integer value
  • ‘%’ operator used to returns the remainder of the division
  • While time is greater then -1 the countdown will run. When times value become 0 then the countdown stop and a sound play that indicate your time is up.

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

  • bd sets the size of the border
  • command calls the function when we click on button

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

Python Countdown Timer Project Output

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.