How to Create a Countdown Timer in Python

How to Create a Countdown Timer in Python


In this tutorial we will create a How to Create a Countdown Timer in Python. This tutorial purpose is to provide a countdown timer for triggering any event. This will cover all the basic parts for creating the countdown timer. I will provide a sample program to show the actual coding of this tutorial.

This tutorial will make it easy for you to understand the coding just follow my instruction I provided and you can do it without a problem. This program is very useful when you want to trigger any event that needed timer. I will try my best to give you the easiest way of creating this program Countdown Timer in python. So let's do the coding.

Getting Started:

First you will have to download & install the Python IDLE's, here's the link for the Integrated Development And Learning Environment for Python

Importing Modules

After setting up the installation and the database, run the IDLE and click file and then new file. After that a new window will appear containing a black file this will be the text editor for the python. Then copy code that I provided below and paste it inside the IDLE text editor.
  1. from tkinter import *

Setting up the Main Frame

After importing the modules, we will now then create the main frame for the application. To do that just copy the code below and paste it inside the IDLE text editor.
  1. root = Tk()
  2. root.title("Countdown Timer")
  3. width = 500
  4. height = 400
  5. screen_width = root.winfo_screenwidth()
  6. screen_height = root.winfo_screenheight()
  7. x = (screen_width/2) - (width/2)
  8. y = (screen_height/2) - (height/2)
  9. root.geometry("%dx%d+%d+%d" % (width, height, x, y))
  10. root.resizable(0, 0)

Creating Main Function

This is the main function of the application is. This code will create a timer that can be trigger with a certain argument. To do this just copy and write these block of codes inside the IDLE text editor.
  1. def countdown(count):
  2. lbl_text['text'] = count
  3. if count > 0:
  4. root.after(1000, countdown, count-1)
  5. elif(count == 0):
  6. global NewForm
  7. NewForm = Toplevel()
  8. NewForm.title("Countdown Timer")
  9. width = 500
  10. height = 300
  11. screen_width = root.winfo_screenwidth()
  12. screen_height = root.winfo_screenheight()
  13. x = (screen_width/2) - (width/2)
  14. y = (screen_height/2) - (height/2)
  15. NewForm.geometry("%dx%d+%d+%d" % (width, height, x, y))
  16. NewForm.resizable(0, 0)
  17. lbl_blast = Label(NewForm, text="TIME EXPIRED!", font=('arial', 50))
  18. lbl_blast.pack(fill=BOTH, pady=100)
  19. btn_back = Button(NewForm, text="Reset", font=('arial', 16), command=BackBtn)
  20. btn_back.pack(side=TOP)
  23. def StartCountdown():
  24. countdown(20)
  25. btn_toggle.config(state=DISABLED)
  28. def BackBtn():
  29. NewForm.destroy()
  30. btn_toggle.config(state=NORMAL)
  31. lbl_text.config(text="20")

In the given code we first create a method called countdown(), it will start the countdown process using the after() function. Then we create a method that will trigger the countdown() function to start the timer called StartCountdown().

Designing the Layout

After creating the Functions we will now add some layout to the application. Just kindly copy the code below and paste it inside the IDLE text editor.
  1. Top = Frame(root, width=500, bd=1, relief=SOLID)
  2. Top.pack(side=TOP)
  3. TextField = Frame(root)
  4. TextField.pack(side=TOP)
  5. BtnGroup = Frame(root, width=500)
  6. BtnGroup.pack(side=BOTTOM)
  8. lbl_title = Label(Top, width=500, text="COUNTDOWN TIMER", font=('times new roman', 16))
  9. lbl_title.pack(fill=X)
  10. lbl_text = Label(TextField, text="20", font=('arial', 200))
  11. lbl_text.pack(fill=BOTH)
  13. btn_toggle = Button(BtnGroup, text="Start", command=StartCountdown)
  14. btn_toggle.pack()

Initializing the Application

After finishing the function save the application as This function will run the code and check if the main is initialize properly. To do that copy and write these block code after the main function inside the IDLE text editor.

  1. if __name__ == '__main__':
  2. root.mainloop()


The How to Create Countdown a Timer in Python source code that I provide can be download below. Please kindly click the download button.

There you have it we successfully created How to Create a Countdown Timer in Python. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

More Tutorials for Python Language

Python Tutorials

Add new comment