Generate a Simple Digital Clock With Python — Tkinter

Handhika Yanuar Pratama
2 min readFeb 11, 2024

--

Photo by Vincent Chan on Unsplash

Time is significant. In the movie “In Time,” time is the way of life; it has become a tradable thing between humans. We don’t create a system that will make times like money. This story will generate a digital clock using Python.

With its versatile libraries, Python allows developers to build graphical user interfaces quickly. Tkinter, the standard GUI toolkit for Python, is particularly well-suited for this purpose. In this article, we’ll create a simple digital clock using Tkinter and the time module.

The Code

Let’s begin by importing the necessary modules.

import tkinter as tk
from time import strftime

Create the function to update the digital clock label. This function formats the current time as a string and updates the text of the digital clock label. Using the after method, The function is scheduled to run again after a 1000-millisecond (1-second) delay.

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

Create the main Tkinter window (root) and title it “Digital Clock.”

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

Configure a label (digital_clock) to display the time. The label’s font, background color, and foreground color are set to make the clock visually appealing. The label is then packed within the window with some padding.

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

Make an initial call to update_time() to set the initial time on the digital clock label.

update_time()

The mainloop() method starts the Tkinter event loop, allowing the window to update continuously based on user interactions or scheduled functions.

root.mainloop()

Source Code

import tkinter as tk
from time import strftime

# Function to update the digital clock label
def update_time():
string_time = strftime('%H:%M:%S %p')
digital_clock.config(text=string_time)
digital_clock.after(1000, update_time)

# Main Tkinter window
root = tk.Tk()
root.title("Digital Clock")

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

# Initial call to update_time function
update_time()

# Tkinter main loop
root.mainloop()

Run The Code

Conclusions

We’ve created a primary digital clock using Python and Tkinter in just a few lines of code. It is only the start; you could use your ideas for further exploration and customization. Tkinter provides a wide range of widgets and options for building more complex graphical applications, making it a powerful tool for GUI development in Python.

--

--

Handhika Yanuar Pratama
Handhika Yanuar Pratama

Written by Handhika Yanuar Pratama

Live the Way the Life ask for || A Stoic Engineer || Technical Writer || Runner x Dreamer

No responses yet