Using Timers and Clocks in TKinter – Python Tkinter GUI Tutorial #79

Posted by


In this tutorial, we will be focusing on how to create timers and clocks using Tkinter in Python. Timers and clocks are commonly used in applications where you need to keep track of time or display a countdown. By the end of this tutorial, you will have a good understanding of how to implement timers and clocks in your Tkinter applications.

To get started, make sure you have Python and Tkinter installed on your system. If you don’t have Tkinter installed, you can install it using the following command:

pip install tk

Now, let’s create a new Python script and import the necessary modules:

import tkinter as tk
from time import strftime

Next, let’s create a basic Tkinter window with a label to display the time:

root = tk.Tk()
root.title("Tkinter Timer and Clock Tutorial")

label = tk.Label(root, font=('calibri', 40, 'bold'), background='black', foreground='white')
label.pack(anchor='center')

Now, let’s define a function called time that will update the label with the current time:

def time():
    string = strftime('%H:%M:%S %p')
    label.config(text=string)
    label.after(1000, time)

In this function, we use the strftime function to get the current time in the format HH:MM:SS AM/PM. We then update the text of the label widget with the current time and call the after method to update the time every second.

Finally, let’s call the time function to start updating the label with the current time:

time()
root.mainloop()

When you run the script, you should see a window displaying the current time that updates every second. This is a simple clock implementation using Tkinter.

Next, let’s create a countdown timer using Tkinter. We will prompt the user to enter the duration of the timer in minutes, and then count down from that duration until it reaches zero.

First, let’s create a new function called countdown that takes the duration of the timer as an argument:

def countdown(duration):
    current_time = duration * 60
    while current_time > 0:
        mins, secs = divmod(current_time, 60)
        timer = '{:02d}:{:02d}'.format(mins, secs)
        label.config(text=timer)
        label.update()
        time.sleep(1)
        current_time -= 1

In this function, we first calculate the total number of seconds based on the duration entered by the user. We then loop through each second, converting it to minutes and seconds, updating the label with the remaining time, and decrementing the current time by 1 every second.

Now, let’s prompt the user to enter the duration of the timer using a simple entry widget and a button:

entry = tk.Entry(root, font=('calibri', 20))
entry.pack()

button = tk.Button(root, text='Start Timer', command=lambda: countdown(int(entry.get())))
button.pack()

When the user clicks the "Start Timer" button, we call the countdown function with the duration entered by the user.

That’s it! You now have a basic countdown timer implemented in Tkinter. You can further customize the appearance of the window, label, and buttons to make it more visually appealing.

I hope this tutorial was helpful in understanding how to create timers and clocks using Tkinter in Python. Try experimenting with different functionalities and design elements to create your own customized timers and clocks for your applications. Happy coding!

0 0 votes
Article Rating

Leave a Reply

24 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@Codemycom
2 hours ago

▶️ Watch Entire Tkinter Playlist ✅ Subscribe To My YouTube Channel:
http://bit.ly/2UFLKgj http://bit.ly/2IGzvOR
▶️ See More At: ✅ Join My Facebook Group:
https://Codemy.com http://bit.ly/2GFmOBz
▶️ Learn to Code at https://Codemy.com ✅ Buy a Codemy T-Shirt!
Take $30 off with coupon code: youtube1 http://bit.ly/2VC9WUN

@problemer3967
2 hours ago

Your videos is very helpful, but i can't figure out how to make a stopwatch that start with 00:00:00. All those videos in YouTube didn't work. Can you make a video out of it? It would be really great!

@tania020
2 hours ago

Really useful. Thanks

@adeleyeadenola7769
2 hours ago

Nice teaching
likes to be your students

@DavidFrankland
2 hours ago

you can construct the whole label text without concatenating the separate components, like:

time.strftime('%H:%M:%S')

@EFoxVN
2 hours ago

Hey there, thanks for this video. Just a question: Would it be necessary to use 'threading' if we use other things on our GUI as well with this function? I know it is often the case.

@frontogaming3338
2 hours ago

Buddy pls tell how to change it in 12 hours format

@TomKnudsen
2 hours ago

How can I ask the creator a question?

@kionmahuermicio9860
2 hours ago

It works with RTC or the Internet?

@justspacing9031
2 hours ago

Hello, I made a pomodoro timer, but I want the window to come out on top every time a work, or break timer is finished. How do I do that?

@shreyaankrao968
2 hours ago

Hello sir, warm greeting from India. Could you give some insights to include a on display timer for each question of a mcq quiz?

@pranavmarella6136
2 hours ago

I don't know what's happening with my code when I run it, but my clock is 5 hours a head of the time!

@jenilchudgar
2 hours ago

101 Comment!
Please heart!

Love from India!

@ab123-j7v
2 hours ago

Great video, can i use it with 24h , .after(86400000 ms) , and if i cant can i use schedule with tkinter.

@bhavesh9378
2 hours ago

Sir the entire program is cool but there is an issue. I ran the program and observed for minute and I found the issue in seconds label…sometimes it icrements 2 seconds… So on that I found solution ie instead of 1000 put 1 ie delay of 1ms … it works fine

@RyanDanielG
2 hours ago

Hit that like button Python Fans! Great tutorial. Feed the Algorithm!

@chironpugh162
2 hours ago

How do I time text while using canvas?

@Xboerefijn1
2 hours ago

Thanks for the explanation, got my GUI all up and running just like I want to now with everything keeping up to date with whatever happens in the big system I'm monitoring.

@sanskargr9347
2 hours ago

how to do analogue clock in tkinter

@shashankbingi3436
2 hours ago

Can we code for flipclock using python

24
0
Would love your thoughts, please comment.x
()
x