Home Python How to make a digital clock in Python

How to make a digital clock in Python

by Humphrey

In this article, we’ll show you how to use Python to create your digital clock. We’ll do this using Tkinter. As we all know, Tkinter is used to make a wide range of GUI (Graphical User Interface) applications. We’ll learn how to make a digital clock with Tkinter in this demo.

Python Digital Clock – What’s needed?

The main prerequisites for creating a digital clock in Python include:

  • Time module
  • Python functions
  • Tkinter basics (Label Widget)

If you’re using Windows, you won’t need to install anything because our simple digital clock software will just use the time and Tkinter modules that are already installed.

However, if you’re using a Linux operating system, the pre-installed Python interpreter may not have Tkinter, so you’ll have to manually install it, as demonstrated below.

Tkinter Module

Tkinter is Python’s standard GUI library. Tkinter is named after the Tk interface. When Python is used in conjunction with Tkinter, it is possible to easily construct graphical user interfaces. The Tk GUI toolkit has a sophisticated object-oriented interface called Tkinter. The latter is a Python binding. In fact, the toolkit is handy in creating graphical user interfaces. If you don’t already have it, you can install it by running the following command that uses the pip package manager.

pip install tkinter


sudo apt-get install python3-tk


The Time module has several methods for obtaining time. This article will use strftime() to convert the current time to the Hour: Minutes: Seconds format.

The actual implementation phase

We’ll use geometry() to provide the visible window’s dimensions and mainloop() to prevent the displayable window from disappearing too rapidly in this code.

The application window’s design

In this stage, we’ll use the Tkinter library to define the window panel. After that, we’ll decide on the text design we’ll utilize for the digital clock.

Design of the label

This is the program’s coolest step. Because you can customize the design to your liking, this stage will set your work apart from the competition. It’s time to put your creativity to the test if you enjoy inventing stuff.

We will customize the following four elements:

  • The font used to display digital digits.
  • The color of our digital clock’s backdrop.
  • Make sure the color of the digital digits isn’t the same as your background.
  • The text’s text border width.

For instance,

font_of_text= ("Boulder", 59, 'bold')
the_background = "#f2e750"
the_foreground= "#363529"<br>width_of_border = 32

Feel free to use RGB or hex values for colors. In some situations, you can opt to use the color’s hex values. We use the color picker provided by Google in the browser. Simply type “Color picker” onto Google.
Let’s put the pieces together and define the label. The text that will display our time is the label function.

label = Label(app_window, font=font_of_text, bg=the_background , fg=the_foreground, bd=width_of_border )
label.grid(row=0, column=1)

Function of a Digital Clock

If we’re working on an application, functions are the most efficient approach to get things done. Functions are also beneficial since they help to structure and understand the program.

Starting the program

This is fantastic! You’ve made it to the end of our application project’s final stage. Functions, as you may know, do not run unless they are called. We’ll use the function to start the application. Let’s have a look at how to use the app:


Overall Understanding of making a digital Clock

The concept is simple: first, we’ll use Tkinter to build an empty window, then we’ll configure and position a Label widget within that window, and last, we’ll update the value of the label to be the current time every 0.08s.

What is the significance of 80 milliseconds?

According to studies, the human brain can only comprehend 12 discrete images per second; anything more than that is interpreted as a single image, causing the illusion of motion.

Below is the complete code for our digital clock, which you may experiment with altering the code parameter as desired and then pressing run again.

Creation of the Digital Clock in Python
from tkinter import *
from tkinter.ttk import *

from time import strftime

main = Tk()

main.title('The Digital clock in Python')

def clock():
	tick = strftime('%H:%M:%S %p')

	clock_label .config(text =tick)

	clock_label .after(1000, clock)

clock_label = Label(main, font =('sans', 80), background = 'yellow', foreground = 'green')

clock_label.pack(anchor= 'center')

digital clock in Python
digital clock in Python

Code walkthrough:

The first step entails importing the packages needed as follows.

from tkinter import *
from tkinter.ttk import *
from time import strftime

The next stage is to create UI for the digital clock as follows.

main = Tk()

The title method is responsible for setting the title for our clock.

main.title('The Digital clock in Python')

Let’s now define a way for obtaining the time. We will call it a clock. To get the time, we’ll use the strftime method and store it in a string. We’ll call the string tick. It’s now time to give the time format.

def clock():
  tick = strftime('%H:%M:%S %p')

Now we’ll use the config method to set the label.

clock_label .config(text = tick)

We can now call our clock function and do the same with the after method. We’d like to call it every second, so that’ll be the first argument we’ll use.

clock_label .after(1000, clock)

We’ll need a label once we’ve finished crafting the title. Our title will be saved on the label. Let’s start by making a label. We’ll accomplish the same thing with the label approach.

clock_label = Label(root, font=('ds-digital', 100), background = 'yellow', foreground = 'green')

The design phase involves picking a typeface and a color for the background and foreground. We’re ready to ship our label now that we’ve finished designing it. We’ll use the pack approach to accomplish this. The anchor method can also be used to define the label’s alignment.

clock_label .pack(anchor='center')

Let’s call our clock function now, and then we’ll call mainloop at the end. That’s all there is to it!


Example 2: How to create a digital clock using Tkinter

# importing the entire tkinter module
from tkinter import *
from tkinter.ttk import *

# to retrieve the system's time, you need to import the strftime function
from time import strftime

# creation of the tkinter window
main = Tk()
main.title('The digital clock in Python')

# This function displays the current time on the label defined herein
def displayTime():
	string = strftime('%H:%M:%S %p')
	clock_label.config(text = string)
	clock_label.after(1000, displayTime)

# creating an attractive look:  needs styling the label widget
clock_label = Label(main, font = ('calibri', 42, 'bold'),
			background = 'purple',
			foreground = 'white')

# defining how to place the digital clock at the centre tkinter's window
clock_label.pack(anchor = 'center')


Example 2: How to create a digital clock using Tkinter
Example 2: How to create a digital clock using Tkinter

Example 3: how to create a digital clock using Tkinter

# start by importing all the necessary libraries

from tkinter import *
import sys
import time #library to get the current time
#create a function displayTime and variable time_now
def displayTime():
    #show the current hour,minute,seconds
    time_now = time.strftime("%H : %M : %S")
    #clock configuration
    #after every 200 microseconds the clock will change
#Creation of a variable responsible for storing the tkinter window
#window size defined

#First label - shows the time,
#Second label - shows hour:minute:second,
#Third label - shows the digital clock's title at the top
#creation of a digital clock's variable
digital_clock_title=Label(main,text="The Digital Clock in Python",font="times 24 bold")
hours_mins_secs=Label(main,text="Hours        Minutes        Seconds",font="times 15 bold")
Example 3: how to create a digital clock using Tkinter
Example 3: how to create a digital clock using Tkinter


This is how to make a simple Digital Clock in Python! Is it simple? So, what exactly are you waiting for? Make your own by experimenting with the code! Make the variations to suit your liking. Finally, we would be glad to know how that goes for you.

You may also like

Leave a Comment