Python Tkinter Digital Clock: A Quick and Popular Project

Posted by


In this tutorial, we will be creating a digital clock using Python’s Tkinter library. Tkinter is a popular library for creating graphical user interfaces in Python, and it provides a simple and easy-to-use interface for creating windows, buttons, labels, and other GUI elements.

To get started, make sure you have Python installed on your computer. You can download Python from the official website (https://www.python.org/downloads/), and be sure to select the option to add Python to your system PATH during installation.

Once you have Python installed, you can install Tkinter by running the following command in your terminal or command prompt:

pip install tk

Now that you have Tkinter installed, you can start creating your digital clock. Open your favorite text editor or IDE and create a new Python script. You can name the script "digital_clock.py".

First, import the necessary modules:

import tkinter as tk
from time import strftime

Next, create a function to update the time displayed on the clock:

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

In this function, we use the strftime() function from the time module 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 use the after() method to call the update_time() function every 1000 milliseconds (1 second).

Now, create the main window and add a label widget to display the time:

root = tk.Tk()
root.title('Digital Clock')

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

update_time()

root.mainloop()

In this code, we create a Tkinter window with the title ‘Digital Clock’. We then create a label widget with a large, bold font and black background with white text. We use the pack() method to display the label in the center of the window with some padding.

Finally, we call the update_time() function to start updating the clock, and we start the main Tkinter event loop with the mainloop() method.

Save the script and run it using your Python interpreter. You should see a window pop up with a digital clock displaying the current time. The time will automatically update every second.

Congratulations! You have successfully created a digital clock using Python’s Tkinter library. Feel free to customize the appearance of the clock by adjusting the font, colors, and layout of the label widget. You can also explore more advanced features of Tkinter to add additional functionality to your clock, such as alarm settings or a stopwatch.

I hope you found this tutorial helpful and informative. Happy coding!

0 0 votes
Article Rating
10 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@tacomiester
1 month ago

An update function that calls itself that makes so much goddamn sense

@ottogrotto5705
1 month ago

Idk if it's just me or the quality of the video is so bad I can barely see anything, but I did see you used "from tkinter import *" and "from time import *" imo, that is a very bad practice which could (And at some cases would) cause overlapping function names which will bring to unexpected behavior in bigger projects

A little tip, import only what you need, that way you'll be saving yourself such a massive headache trying to figure why a function can't take specific params (Usually **Kwargs – Keyword arguments) just to figure the interpreter reads the wrong function

A little codeless example;
I have a class (For this example I'll call it class A) and class A has a function called sleep, I imported the function from the class while also importing everything from the time module (* means everything from that module) now, when I try to use the sleep function from class A, it might call the sleep function from the time module, which isn't what I wanted i to do, I needed the time module for something else entirely

Just a little point to think about before importing everything

@mr.duffer3570
1 month ago

Bro what is the name of this software it not visual code

@vaishnavipatil2946
1 month ago

Thank you 😊 Very helpful

@codexinfo6318
1 month ago

Good keep going 😇

@M_513
1 month ago

from tkinter import *
from time import *

w = Tk()
w.title("Digital clock")
w.geometry("420×150")
w.resizable(1,1)
I = Label(w,
font=("Arial",68, "bold"),
bg="yellow",
fg="red",
bd=25)

L.grid(row=0, column=1)

def update():
t = strftime("%H.%M:%S")
I.config(text=t)
w.after(1000, update)

update()
w.mainloop()

Where the fulse

@Liamallen125
1 month ago

Thanks. Now I know how to use Tkinter more

@ujjwal5541
1 month ago

pls can you provide the sorce code or pls share the code once here

@someonewhodied2222
1 month ago

At l.grid it shows that tuple object doesn't have grid attributes

@beeeeeee42333
1 month ago

isn't from tkinter import * , puts hell kinda memory issues on run time ?