In this lesson i want to show How to Create Countdown Timer with Python & Tkinter, Creating countdown timer with Python and Tkinter involves differetnt steps:
Now let’s talk about the steps
- Import the necessary libraries: you need to import the Tkinter library for creating the GUI and the time library for implementing the countdown timer.
1 2 |
import tkinter as tk import time |
- Create the main window: you can create the main window of the application using the Tk() function. you can set the title, size and other properties of the window.
1 2 3 |
root = tk.Tk() root.title("Countdown Timer") root.geometry("200x200") |
- Create the label: you can create label that will display the countdown timer using the Label() function. You can set the font, size and other properties of the label.
1 2 |
label = tk.Label(root, font=("Courier", 30)) label.pack() |
- Create the countdown function: you can create function that will implement the countdown timer. function should take the total time in seconds as an input and update the label every second with the remaining time. you can use the after() method to schedule the function to be called again after certain period of time.
1 2 3 4 5 6 |
def countdown(count): if count > 0: label.config(text=str(count)) label.after(1000, countdown, count-1) else: label.config(text="Time's up!") |
- Start the countdown: you can start the countdown by calling the countdown() function with the total time in seconds as an input.
1 |
countdown(10) |
- Run the application: You can run the application by calling the mainloop() function.
1 |
root.mainloop() |
This is the complete code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import tkinter as tk import time root = tk.Tk() root.title("Countdown Timer") root.geometry("200x200") label = tk.Label(root, font=("Courier", 30)) label.pack() def countdown(count): if count > 0: label.config(text=str(count)) label.after(1000, countdown, count-1) else: label.config(text="Time's up!") countdown(10) root.mainloop() |
The above is basic example, you can customize it as you need, you can add start, stop, pause and reset buttons, you can also add an input to let the user enter the time instead of hard coding it, you can also add a progress bar, and more.
Please note that this is a basic example, and there are many ways to create a countdown timer with Python and Tkinter. The specific implementation will depend on your use case.
Learn More on Python GUI Development
- Build Text to Speech App with Python & PyQt5
- How to Build GUI Window in PyQt6
- How to Show Image in PyQt6
- How to Create Calendar with Python & PyQt6
- How to Create CheckBox in PyQt6
Run the complete code and this will be the result.