How to Build a Digital Clock in Python using Tkinter – Step-by-Step Tutorial

Build A Digital Clock In Python

Introduction

Creating a digital clock in Python is one of the simplest yet effective projects for beginners learning GUI development. In this tutorial, we will walk you through building a real-time digital clock using Tkinter, Python’s standard GUI library. This project is a great way to enhance your understanding of time handling and GUI design in Python.

By the end of this guide, you’ll have a fully functional digital clock that updates every second, all in under 20 lines of code!

Prerequisites:

Before we dive into the code, make sure you meet the following prerequisites:

Basic knowledge of Python syntax

Python 3 is installed on your machine

Familiarity with functions and loops

No prior GUI experience is necessary!

What is Tkinter?

Tkinter is Python’s standard GUI (Graphical User Interface) package. It provides various widgets, such as buttons, labels, and entry boxes, to build interactive applications. Tkinter is easy to use and does not require additional installations for standard Python distributions.

Installation

Tkinter is included with most Python distributions. You can check if it’s installed by running this command in your Python shell:

import tkinter

If there are no errors, you’re good to go. If not, you can install it (Linux users) via:

sudo apt-get install python3-tk

For Windows and Mac, Tkinter usually comes pre-installed with Python 3.

Project Overview

In this project, we’ll build a GUI-based digital clock that:

  • Displays current time in HH:MM:SS format

  • Updates every second

  • Has a clean and minimalist black-and-cyan design

Step-by-Step Code Explanation

Let’s walk through the code step by step.

1. Import Libraries


import tkinter as tk
from time import strftime
  

We import tkinter for GUI elements and strftime from the time module to fetch the current time.

2. Create the Main Window


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

We create the root window and give it a title.

3. Define the Clock Label


clock_label = tk.Label(root,
font=("Helvetica", 48),
bg="black", fg="cyan")
clock_label.pack(anchor="center", fill="both", expand=True)
  

This label will display the current time with specified font size and colors. The pack() method ensures the label takes up available space.

4. Create a Function to Update Time


def update_time():
    current_time = strftime("%H:%M:%S")
    clock_label.config(text=current_time)
    clock_label.after(1000, update_time)
  

This function fetches the current time and updates the label every second using after().

5. Run the Application


update_time()
root.mainloop()
  

We call the update_time() function and start the GUI loop using mainloop().

Complete Source Code

Here is the full code for easy copy-paste:


import tkinter as tk
from time import strftime

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

# Define the clock label
clock_label = tk.Label(root,
                       font=("Helvetica", 48),
                       bg="black", fg="cyan")
clock_label.pack(anchor="center", fill="both", expand=True)

# Function to update the time
def update_time():
    current_time = strftime("%H:%M:%S")
    clock_label.config(text=current_time)
    clock_label.after(1000, update_time)

update_time()
root.mainloop()
  
clock

Output Preview

When you run the script, a black window with a large cyan digital clock will appear, updating the time every second.

clock

Conclusion

This simple yet powerful project is a great way to get started with Python GUI programming. You can customize the font, background color, or even extend it to show the date and day. Building a digital clock with Tkinter is not just educational—it’s a stepping stone to bigger desktop apps!

FAQs

Q1. Can I change the clock format to 12-hour with AM/PM?

Yes! Modify the strftime line as:


current_time = strftime("%I:%M:%S %p")
  

Q2. How can I display the date as well?

You can add another Label for the date using:


date_label = tk.Label(root, font=("Helvetica", 20), bg="black", fg="white")
date_label.pack()
  

And update it similarly in the update_time() function.

Q3. Is this compatible with Python 2?

Tkinter in Python 2 is imported with a capital “T”:


import Tkinter as tk
  

However, it’s recommended to use Python 3 for this project.

Post You May Also Like: