IBepyProgrammer

How to Build a Digital Clock In Python

2 min read
How to Build a Digital Clock In Python

In this tutorial, we will learn how to build a digital clock in Python. We will be utilizing the Tkinter library to build the graphical user interface of the clock.
Tkinter is the default user interface library in Python. Tkinter comes bundled with Python hence there is no need to install the library using pip.
In later tutorials, we will delve deeper into Tkinter and learn how to build more complex applications for an even deeper understanding of the library.

Step 1

For this example, we will only import the method "strftime" from the module time.
Note that the method "strftime" returns a string representing the current date and time.
We will then proceed to import the tkinter library.
The "Tk" method allows us to create the main window using the Tkinter library while the "Label" specifies the container box where we would place text or images.

from time import strftime
from tkinter import Tk, Label

Step 2

We then proceed to create the window for the digital clock. This is done by storing the Tk() method within a variable that I named clock_window.

clock_window = Tk()
clock_window.title("Python Clock")
clock_window.geometry("300x100")
clock_window.configure(bg = "black")
clock_window.resizable(False, False)

The title of the window will then be named "Python Clock".
The "clock_window.geometry()" specifies a fixed size of the window that we are creating to be 300 by 100.
The "clock_window.configure(bg="black")" allows us to change the background color of the tkinter window as the default color is grey.
The clock_window.resizable(False, False) allows us to set a non-resizable window for both height and width options.

Step 3

We then configure the label of the clock using the Label method we imported from the Tkinter library.
Since we are going to display the Label directly in the Tkinter window, we will pass the clock_window object we created in the previous step.

  • The value "bg" stands for the background which will create a Label with a black background color, and will then be displayed in the main window.
  • The value "fg" stands for the foreground which will create a Label with white foreground color, and will then be displayed in the main window.
  • The font allows us to adjust text properties such as the font family and font weight.
  • The relief value refers to certain effects around the outside of the widget. Other effects that can be utilized include:
    1. RIDGE
    2. RAISED
    3. GROOVE
    4. SUNKEN
clock_label = Label(clock_window, bg = "black", fg = "white", font = ("Helvetica", 36, "bold"), relief = "flat")
clock_label.place(x = 20, y = 20)

Step 4

We then create a function that will update the labels in the clock_window with the current date and time.
As stated previously we use the method "strftime" to get the current date and time in hours, minutes, and seconds.

def update_labels():
    current_time = strftime("%H: %M: %S")
    clock_label.configure(text = current_time)
    # Note that the clock is updated every 80 milliseconds.
    clock_label.after(80, update_labels)

Step 5

To display the clock we created, we simply run the function above.
We also run the mainloop() function to prevent the clock widget from terminating immediately.

update_labels()
clock_window.mainloop()

Conclusion

In this short article, we briefly introduce the Tkinter library in Python and learn how to use it to build a simple digital clock. In future articles, we will dive deeper into Tkinter to build more complex graphical user interfaces to gain a deeper insight into the module.
If you found this article helpful consider subscribing and sharing. A copy of the working code can also be found on my Git hub repository. https://github.com/IBepyProgrammer/Python-Digital-Clock

Thank you.

Sign up for our newsletter

Don't miss anything. Get all the latest posts delivered to your inbox. No spam!