7-Segment Digital Clock using Python Tkinter
Digital clocks are an interesting way to practice Python GUI programming. In this project, we build a 7-segment digital clock using Python Tkinter. Instead of showing plain text time, this clock uses a 7-segment style display, similar to the ones seen in digital watches, calculators, and old LED clocks.
Why Build a 7-Segment Clock in Python?
Most Python clocks simply display time using labels, but this project is unique because it mimics the classic 7-segment LED design. This helps beginners understand:
- How to draw custom shapes (segments) in Tkinter Canvas.
- How to map digits into 7-segment format.
- How to update time dynamically using
after()
.
Key Features of the Project
- 7-Segment Digits: Each number is drawn with 7 polygons, simulating LED segments.
- Digital Clock Functionality: Shows the current system time in 12-hour format with AM/PM.
- Colon & AM/PM Display: Adds colon dots and AM/PM letters for a realistic clock.
- Real-Time Update: Refreshes every second using Tkinter’s
after()
function. - Stylish Design: Black background with glowing red digits for a retro digital effect.
🌐 Digital Clock
import tkinter as tk
from time import strftime
root = tk.Tk()
root.title("7-Segment Digital Clock")
root.configure(bg="black")
# Window size
root.geometry("300x200")
root.resizable(False, False)
canvas = tk.Canvas(root, bg="black", width=300, height=200, highlightthickness=0)
canvas.pack()
# Segment map
digit_segments = {
"0": [1,1,1,1,1,1,0],
"1": [0,1,1,0,0,0,0],
"2": [1,1,0,1,1,0,1],
"3": [1,1,1,1,0,0,1],
"4": [0,1,1,0,0,1,1],
"5": [1,0,1,1,0,1,1],
"6": [1,0,1,1,1,1,1],
"7": [1,1,1,0,0,0,0],
"8": [1,1,1,1,1,1,1],
"9": [1,1,1,1,0,1,1]
}
def draw_segment(x, y, seg_id, size, color):
"""Draw one segment polygon"""
thickness = size // 6
length = size
if seg_id == 0: # top
coords = [(x, y), (x+length, y), (x+length-thickness, y+thickness), (x+thickness, y+thickness)]
elif seg_id == 1: # top-right
coords = [(x+length, y), (x+length, y+length), (x+length-thickness, y+length-thickness), (x+length-thickness, y+thickness)]
elif seg_id == 2: # bottom-right
coords = [(x+length, y+length), (x+length, y+2*length), (x+length-thickness, y+2*length-thickness), (x+length-thickness, y+length+thickness)]
elif seg_id == 3: # bottom
coords = [(x, y+2*length), (x+length, y+2*length), (x+length-thickness, y+2*length-thickness), (x+thickness, y+2*length-thickness)]
elif seg_id == 4: # bottom-left
coords = [(x, y+length), (x+thickness, y+length+thickness), (x+thickness, y+2*length-thickness), (x, y+2*length)]
elif seg_id == 5: # top-left
coords = [(x, y), (x+thickness, y+thickness), (x+thickness, y+length-thickness), (x, y+length)]
elif seg_id == 6: # middle
coords = [(x+thickness, y+length-thickness//2), (x+length-thickness, y+length-thickness//2),
(x+length, y+length), (x+length-thickness, y+length+thickness//2),
(x+thickness, y+length+thickness//2), (x, y+length)]
else:
return
canvas.create_polygon(coords, fill=color, outline=color)
def draw_digit(x, y, digit, size=25, color_on="red", color_off="#220000"):
segs = digit_segments[digit]
for i in range(7):
color = color_on if segs[i] else color_off
draw_segment(x, y, i, size, color)
def update_clock():
canvas.delete("all")
now = strftime("%I:%M:%S %p") # 12-hour format with AM/PM
x = 10
for ch in now:
if ch == ":":
# colon dots
canvas.create_oval(x, 40, x+5, 50, fill="red", outline="red")
canvas.create_oval(x, 70, x+5, 80, fill="red", outline="red")
x += 20
elif ch == " ":
x += 20
elif ch in ["A","P","M"]:
canvas.create_text(x, 120, text=ch, fill="red", font=("Arial", 12, "bold"))
x += 15
else:
draw_digit(x, 20, ch, size=25)
x += 40
root.after(1000, update_clock)
update_clock()
root.mainloop()
Step-by-Step Code Explanation
1. Tkinter Window Setup
We create a Tkinter window of size 300x200
with a black background.
The Canvas
widget is used to draw the segments and time display.
2. Digit Segment Mapping
Each digit (0–9) is represented by a list of 0
and 1
,
where each value indicates whether a segment should be ON or OFF.
Example: "8": [1,1,1,1,1,1,1]
means all 7 segments are lit.
3. Drawing Segments
The draw_segment()
function creates polygon shapes for each segment
(top, bottom, left, right, and middle). Depending on the digit, segments are either red (ON) or dim (OFF).
4. Updating the Clock
The update_clock()
function:
- Fetches current time using
strftime("%I:%M:%S %p")
. - Draws each digit using the 7-segment display logic.
- Adds colons and AM/PM text.
- Runs every second with
root.after(1000, update_clock)
.
Applications of This Project
- A fun Python GUI project for beginners.
- Can be used in Python desktop applications that need a digital clock.
- Helps in understanding Canvas drawing and custom UI elements.
- Good practice for time-based event handling in Tkinter.