How to create a digital clock with python easily

This tutorial is about creating a project using python language. I’m going to show you how to create a digital clock with Python. Before reading the article, if you like to know more about Python, you could check out these two important articles:

This project is suitable for beginners. To write the code, you need to know how to use modules in Python. Two necessary modules that I use in this project are Tkinter and time. Tkinter is a GUI library that helps you develop a graphical user interface for the digital clock, and using the time module; you could get the current time, date, timezone, etc. Let’s dive deep into these modules.

The video

Tkinter module

Tkinter is the standard GUI library for Python. The combination of Python and Tkinter provides a fast and straightforward approach to producing GUI applications. Since Tkinter provides a powerful object-oriented interface to the Tk GUI toolkit, Creating a GUI application using Tkinter is an easy task. In this project, just some parts of this module are used. We need to create a window, give it a title, set its geometry, and put it in the main loop. Look at the following loop:

root = Tk()
root.title('Digital Clock')
root.geometry('800x400')
root.mainloop()
  1. To make a window, it is necessary to create an object of Tk() class in line one.
  2. In second line, the window is given a “Digital Clock” title using the title method.
  3. Line 3 is related to the size of the window. So, we set it equal to 800×400 pixels.
  4. Line 4 is crucial since without main loop method, the window won’t show up. Actually, this mehtod causes the window to run until some body presses the “x” button.

At this point I recommend you to write the above code and see the result. After creating the proper window, it’s time to give it some text. This is done by the “Label” function and “pack” method. Look at the following code:

myLabel = Label(root, text='Hello world!', font=(
    'Arial', 72), fg='white', bg='black')
myLabel.pack()
  1. The “Label” function sets the text, gives it font name and font size and set its foreground and background color. Moreover, the name of related window must be given to Label function. The output of this function should be stored in a variable.
  2. The myLabel variable is an object of Label, so using its pack method, the text will show up on the window.

Time module

The Python time module provides many approaches to represent time in code, such as objects, numbers, and strings. It also provides functionality other than representing time, such as measuring the efficiency of the code and waiting during the execution of the code. To use the time module in this project look at the following code:

def myTime():
    myText = time.strftime("%I:%M:%S %p")
    myLabel.config(text=myText)
  1. We need to create the “myTime” function and extract hour, minute, and second, using the “strftime” method.
  2. This time is stored in a variable as a string; then, it needs to be added to the window’s text by config method.

There is still one tricky code that we need to add to the function in the following code snippet:

def myTime():
    ...
    myLabel.after(1000, myTime)

This part causes the “myTime” function to be called every 1000 milliseconds or one second. Don’t forget to call this function before the main loop. Now, we are ready to put it all together and make it work, so let’s jump into the main program.

Digital clock code

After explaining all parts of this project individually, it’s time to write all of it and see how it works in practice. Look at the following code:

from tkinter import *
import time

root = Tk()
root.title('Digital Clock')
root.geometry('800x400')

def myTime():
    myText = time.strftime("%I:%M:%S %p")
    myText2 = time.strftime("%A")
    myLabel.config(text=myText)
    myLabel2.config(text=myText2)
    myLabel.after(1000, myTime)

myLabel = Label(root, text='', font=(
    'Arial', 72), fg='white', bg='black')
myLabel.pack()
myLabel2 = Label(root, text="", font=('Arial', 24))
myLabel2.pack()

myTime()
root.mainloop()
  1. At first two modules need to be imported.
  2. Then an object of Tk() class is created and it’s title and geometry is set.
  3. After that “myTime” function need to read the current time.
  4. Moreover, two labels are alocated to root window.
  5. Finally, “myTime” function is called and run in the mainloop.

The output

When you run the code, you can see the output window like this:

create a digital clock with python

Final word

After watching the video and reading this article about a Python digital clock example, you are ready to create a digital clock with Python and run it. So, do not hesitate and write your code without any help. Try to figure out any troubles you face. If you have any problem you could not solve, drop a comment below, and I will answer you soon.
If you would like to study m

Was this helpful?
[0]