Creating a Digital Clock in Python with Tkinter


By Faraz - June 02, 2023

Learn how to create a digital clock using Python and Tkinter with this step-by-step tutorial. Build your GUI programming skills and enhance your Python projects.

Digital Clock Python Programming.jpg

In this article, we will explore how to create a digital clock using Python and Tkinter. Tkinter is a standard Python library for GUI development, providing a wide range of widgets and tools to build interactive applications. By the end of this tutorial, you will be able to develop your own digital clock application using Tkinter.

Table of Contents

  1. Introduction
  2. Understanding Tkinter and its features
  3. Setting up the development environment
  4. Importing the necessary modules
  5. Creating the main window
  6. Designing the digital clock
  7. Updating the clock every second
  8. Running the application
  9. Full Source Code
  10. Conclusion
  11. FAQs

I. Introduction

In today's digital age, clocks have become an integral part of our lives. Whether it's on our mobile devices, computers, or even smartwatches, we rely on clocks to keep track of time. Python, being a versatile programming language, allows us to create graphical user interfaces (GUIs) effortlessly. Tkinter, as the default GUI toolkit for Python, makes it convenient to design and build applications with various widgets, including clocks.

II. Understanding Tkinter and its features

What is Tkinter?

Tkinter is a Python binding to the Tk GUI toolkit, which was originally developed for the Tcl programming language. It provides a set of tools and widgets to create graphical user interfaces. Tkinter is included with Python, making it readily available for developers without the need for additional installations.

Why choose Tkinter for GUI development?

There are several reasons to choose Tkinter for GUI development:

  • Ease of use: Tkinter has a simple and intuitive interface, making it suitable for beginners and experienced developers alike.
  • Cross-platform compatibility: Tkinter applications can run on different operating systems, including Windows, macOS, and Linux, without requiring major modifications.
  • Rich widget library: Tkinter offers a wide range of widgets, such as buttons, labels, entry fields, and more, allowing developers to create feature-rich interfaces.
  • Integration with Python: Tkinter seamlessly integrates with Python, enabling developers to leverage the power of Python's libraries and modules in their GUI applications.

Features of Tkinter

Tkinter provides various features that make GUI development efficient and straightforward:

  • Widgets: Tkinter offers a comprehensive set of widgets to create interactive applications. These include buttons, labels, entry fields, checkboxes, radio buttons, and many more.
  • Layout management: Tkinter provides different layout managers to organize widgets within the application window. These managers allow for flexible arrangement and resizing of widgets.
  • Event-driven programming: Tkinter follows an event-driven programming paradigm, where actions by the user or the system trigger events. Developers can define event handlers to respond to these events.
  • Customization: Tkinter allows for customization of widget appearance through properties such as colors, fonts, and sizes. This enables developers to create visually appealing interfaces.

III. Setting up the development environment

Before we dive into creating the digital clock, let's set up our development environment by installing Python and Tkinter.

Installing Python and Tkinter

To install Python, visit the official Python website (https://www.python.org) and download the latest version compatible with your operating system. Follow the installation instructions provided on the website.

Tkinter is included with Python, so you don't need to install it separately. However, ensure that Tkinter is installed correctly by running the following code in a Python shell:

import tkinter

If no errors occur, Tkinter is successfully installed.

Creating a new Python project

Once Python and Tkinter are installed, create a new directory for your project. Open your preferred code editor and navigate to the project directory. Create a new Python file with a .py extension, such as digital_clock.py, to write our code.

IV. Importing the necessary modules

To create a digital clock, we need to import the tkinter and time modules.

Importing the tkinter module

In the Python file, import the tkinter module using the following code:

import tkinter as tk

We import tkinter and alias it as tk for convenience.

Importing the time module

The time module in Python provides various time-related functions. Import it using the following code:

import time

This module will help us retrieve the current time for the digital clock.

V. Creating the main window

To start building our digital clock application, we need to create the main window.

Creating a new window object

In the Python file, create a new window object using the Tk() constructor provided by Tkinter:

window = tk.Tk()

This will create a new window that serves as the main container for our application.

Configuring the window properties

Next, we can configure the properties of the window, such as its title and dimensions:

window.title("Digital Clock")

Here, we set the title of the window to "Digital Clock".

VI. Designing the digital clock

Now, let's design the digital clock interface by creating a label widget to display the time.

Creating a label widget for displaying the time

In the Python file, create a label widget using the Label() constructor provided by Tkinter:

clock_label = tk.Label(window, font=("Arial", 80))

We create a label widget named clock_label and set its font to "Arial" with a font size of 80. The pack() method is used to place the label widget in the window.

Styling the label widget

To enhance the appearance of the clock, we can customize the label widget's properties, such as the text color and background color:

clock_label.config(fg="cyan", bg="black")

Here, we set the text color (fg) to cyan and the background color (bg) to black. Feel free to customize these colors according to your preference.

VII. Updating the clock every second

A digital clock needs to continuously update the displayed time. We can achieve this by defining a function that retrieves the current time and updates the label widget accordingly.

Defining a function to update the time

In the Python file, define a function named update_time():

def update_time():
    current_time = time.strftime("%I:%M:%S %p")
    window.after(1000, update_time)

This function retrieves the current time using the strftime() function from the time module. The time format "%H:%M:%S %p" represents hours, minutes, seconds, and Locale’s equivalent of either AM or PM. We set the text of the clock_label widget to the current time using the config() method.

Using the after method to schedule the update

To update the time every second, we can use the after() method provided by Tkinter. Add the following line of code at the end of the Python file:

window.after(1000, update_time)

This line schedules the update_time() function to be called every 1000 milliseconds (1 second). It ensures that the clock updates continuously.

VIII. Running the application

Finally, let's run our digital clock application.

Starting the main event loop

At the end of the Python file, add the following code:


This line starts the main event loop of the application, which listens for events such as button clicks or window resizing. It ensures that our application remains responsive.

Now, save the Python file and run it using your preferred Python interpreter. You should see a window displaying the current time as a digital clock. The time will update every second, giving you a functional and dynamic clock application.

IX. Full Source Code

import tkinter as tk

import time

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

def update_time():
    current_time = time.strftime('%I:%M:%S %p')
    window.after(1000, update_time)

clock_label = tk.Label(window, font=("Arial", 80))
clock_label.config(fg="cyan", bg="black")


X. Conclusion

In this article, we have explored how to create a digital clock using Python and Tkinter. We learned about Tkinter and its features, set up the development environment, and built a graphical interface for the digital clock. By utilizing the time module and scheduling updates, we ensured that the clock displays the accurate time in real time. You can further customize the clock's appearance and add additional functionality based on your requirements.

Creating interactive applications like a digital clock using Tkinter is just the beginning of what you can achieve with this powerful GUI toolkit. With Tkinter's extensive widget library and Python's flexibility, you can create a wide range of graphical applications tailored to your specific needs.


Q1. Can I use Tkinter on platforms other than Windows?

A1. Yes, Tkinter is cross-platform and can be used on various operating systems, including Windows, macOS, and Linux.

Q2. Is Tkinter the only GUI toolkit available for Python?

No, there are other GUI toolkits available for Python, such as PyQt, PySide, and wxPython. However, Tkinter comes pre-installed with Python, making it a convenient choice for beginners and lightweight applications.

Q3. Can I change the font and size of the clock in the digital clock application?

Yes, you can customize the font and size of the clock by modifying the font parameter when creating the label widget. Simply change the font name and size to your desired values.

Q4. How can I add additional features to the digital clock application?

You can extend the digital clock application by incorporating additional widgets and functionality. For example, you could add buttons to start or stop the clock, display the date alongside the time, or implement alarms and reminders.

Q5. Are there any online resources to further learn about Python and Tkinter?

Yes, there are numerous online tutorials, documentation, and forums dedicated to Python and Tkinter. Websites such as Python.org, TkDocs.com, and Stack Overflow provide valuable resources for learning and troubleshooting.

That’s a wrap!

I hope you enjoyed this article

Did you like it? Let me know in the comments below 🔥 and you can support me by buying me a coffee.

And don’t forget to sign up to our email newsletter so you can get useful content like this sent right to your inbox!

Faraz 😊

End of the article

Subscribe to my Newsletter

Get the latest posts delivered right to your inbox

Related Post