Python to create a digital clock using Tkinter

Python Tkinter can be used to create all kinds of GUI programs for desktop applications. In this article, we will see how to create a digital clock that displays the current time in hours, minutes, and seconds format with live updates.

We use the time module to import the strftime() method, which formats the current time. The clock updates automatically every 200 milliseconds using a recursive function and Tkinter's after() method.

Creating the Digital Clock

Here's how to build a simple digital clock using Tkinter ?

import time
from tkinter import *

# Create the main window
canvas = Tk()
canvas.title("Digital Clock")
canvas.geometry("350x200")
canvas.resizable(1,1)

# Create a label to display the time
label = Label(canvas, font=("Courier", 30, 'bold'), bg="blue", fg="white", bd=30)
label.grid(row=0, column=1)

# Function to update the clock
def digitalclock():
    text_input = time.strftime("%H:%M:%S")
    label.config(text=text_input)
    label.after(200, digitalclock)  # Update every 200ms

# Start the clock
digitalclock()
canvas.mainloop()

How It Works

The digital clock works through these key components:

  • time.strftime("%H:%M:%S") − Formats current time as HH:MM:SS
  • label.config(text=text_input) − Updates the displayed text
  • label.after(200, digitalclock) − Schedules the next update after 200ms
  • Recursive function − digitalclock() calls itself to create continuous updates

Customizing the Clock

You can customize the appearance and format ?

import time
from tkinter import *

canvas = Tk()
canvas.title("Custom Digital Clock")
canvas.geometry("400x150")
canvas.configure(bg="black")

# Custom styling
label = Label(canvas, 
              font=("Arial", 25, 'bold'), 
              bg="black", 
              fg="lime", 
              bd=10)
label.pack(expand=True)

def digitalclock():
    # Include date and time
    current_time = time.strftime("%Y-%m-%d\n%H:%M:%S")
    label.config(text=current_time)
    label.after(1000, digitalclock)  # Update every second

digitalclock()
canvas.mainloop()

Output

Running the code creates a window with a digital clock that displays the current time and updates automatically. The clock shows hours, minutes, and seconds in a bold Courier font with a blue background.

Digital Clock 14:32:07

Conclusion

Creating a digital clock with Tkinter involves using time.strftime() for time formatting and label.after() for automatic updates. The recursive function ensures continuous time display, making it perfect for desktop applications.

Updated on: 2026-03-15T18:21:20+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements