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.
Keep Reading
Related Articles
Learning Guides
INNER JOIN in SQL with Examples: Complete Beginner's Guide
Learn INNER JOIN in SQL with practical examples, syntax, interview questions, and real-world use cases. A complete beginner-friendly SQL JOI
Learning Guides
OUTER JOIN in SQL: Complete Guide with Examples for Beginners
Master OUTER JOIN in SQL with practical examples and real-world scenarios. Learn LEFT JOIN, RIGHT JOIN, FULL OUTER JOIN, syntax, use cases,
Learning Guides
Multi-Row Functions in SQL: Complete Guide with Examples
Learn multi-row (aggregate) functions in SQL including SUM, AVG, COUNT, MIN, MAX, GROUP BY and HAVING, with practical examples, NULL handlin