Today we are going to learn how to create a countdown timer in Python. We’ll be using Python IDE to write our code and a few built-in libraries including, time module, turtle module, and tkinter module.
PART 1: MAKING THE TIMER
The time module has a function time.sleep() which we’ll use to define a second. We start with asking how long you want to set the timer for. Let’s keep it short!
mins = int(input("MINUTES?"))
And then let’s define the seconds as 0. You can also print a message saying the timer has started!
secs = 0 print("TIMER HAS STARTED!")
Now we need to import the time library that we’ll be using in our code.
Now of course we want the timer to countdown until it’s 00:00. So we need to use the while loop until the minute digit reaches 0. And we also need to define the format of our timer.
while mins >= 0: print(str(mins).zfill(2) + ":" + str(secs).zfill(2))
Here zfill() method will make sure that there are always two digits in mins and secs digits.
It will add a zero before the digit whenever there is a gap.
And now for the working of our timer, we define seconds as secs = secs – 1 and we use the time.sleep(1). The sleep() function will stop the code for 1 second before executing the next.
So now the seconds’ needle is moving but what about the minute hand? After 00 seconds, it should change the minute digit to a number less than the one before. We can achieve this using an if statement.
if secs = -1: secs = 59 mins = mins - 1
That’s it! You can also print another message saying the timer has stopped!
print("TIMER HAS STOPPED!")
PART 2: MAKING THE DISPLAY
We can easily modify our code to make a nice display of the timer. For that, we’ll be using the turtle module. We could also use Pygame which has much better graphics, but let’s just go for minimalism.
The code below will set up the turtle module for us. It can be added at the very beginning of our code.
from turtle import * setup() tl = Turtle()
Here import * means import everything from that library.
So in the previous code, instead of print we’ll swoop in our turtle! or tl.write.
And to make it look even nicer, let’s change the font and make it a bit larger.
tl.write(str(mins).zfill(2) + ":" + str(secs).zfill(2), font = ("arial", 160, "normal"))
We also need to clear all the previous digits that get printed, otherwise, it will look like this.
To make the screen clearer you can also hide the turtle at the origin in the while loop.
while mins >= 0: tl.hideturtle() tl.clear()
And we are done!
PART 3: MAKING A POPUP MESSAGE
Now your code is running in the background, but how can you know that the timer has stopped? For that, we are going to make a popup message on your screen so you can rest for a bit and be proud of working for so long!
We can use another library tkinter for that. If you add the following piece of code at the end, you’re all set!
from tkinter import * import tkinter.messagebox root=Tk() tkinter.messagebox.showinfo('Timer','TIMER HAS STOPPED!') root.mainloop()
You can play with the code and make a clock by reversing a few things in the code, or you can use some other display method that is even more satisfying for the eye. If you get stuck, you can always google! Good luck!