📢
Admissions Open for August 2026 Batch | Free Career Counselling | Limited Scholarships
Register Now →

Learning Guides

Create a Digital Clock Using Python: Step-by-Step Guide

Quick answer: Learn how to build a simple digital clock in Python using the Tkinter GUI library and the time module, with complete working code.

What You Will Build

A small desktop application that displays the current time and updates every second, using Python's built-in Tkinter library for the interface and the time module to fetch the current time.

Requirements

Tkinter ships with standard Python installations, so no extra installation is usually needed.

Complete Code

import tkinter as tk
from time import strftime

def update_time():
    current_time = strftime('%H:%M:%S')
    clock_label.config(text=current_time)
    clock_label.after(1000, update_time)   # schedule next update in 1000ms

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

clock_label = tk.Label(root, font=('Arial', 40), background='black', foreground='lime')
clock_label.pack(padx=20, pady=20)

update_time()
root.mainloop()

How It Works

strftime for formatting time

strftime('%H:%M:%S') converts the current system time into a readable Hours:Minutes:Seconds string. %H gives 24-hour format; use %I with %p for 12-hour format with AM/PM.

The after() method for repeated updates

label.after(1000, update_time) schedules update_time to run again after 1000 milliseconds, without blocking the rest of the interface. This is the correct way to create a repeating action in Tkinter, rather than using a blocking loop with time.sleep().

root.mainloop()

This starts Tkinter's event loop, which keeps the window open and responsive, and must be the last line of the script.

Extending It: Adding the Date

def update_time():
    current_time = strftime('%H:%M:%S')
    current_date = strftime('%A, %d %B %Y')
    clock_label.config(text=f"{current_time}\n{current_date}")
    clock_label.after(1000, update_time)

Why This Is a Good Beginner Project

It combines several genuinely useful skills at once: working with a GUI library, formatting dates and times, and understanding how a program can repeat an action without blocking, all in under 20 lines of code.

Common Interview Questions

Why use label.after() instead of a while loop with time.sleep()?

time.sleep() would freeze the entire application window during the pause, making it unresponsive. after() schedules the update without blocking the Tkinter event loop, keeping the interface responsive.

What does %H versus %I control in strftime?

%H formats the hour in 24-hour format. %I formats it in 12-hour format, typically paired with %p to show AM or PM.

FAQ

Frequently Asked Questions

What Python library is used to build a digital clock GUI?

Tkinter, which ships with standard Python installations and provides basic GUI elements like labels and windows.

Why not use time.sleep() to update the clock every second?

time.sleep() would freeze the entire application window during the pause. Tkinter's after() method schedules updates without blocking the interface.

How do you show the time in 12-hour format with AM/PM?

Use strftime('%I:%M:%S %p') instead of %H, which gives 24-hour format.

Want This Mapped to Your Own Background?

A free counselling session will tell you which path fits, and will tell you honestly if none of ours does.

Book Free Career Counselling

Keep Reading

Related Articles