Digital clock using PyQt5 in Python

In this post, we'll see at how to make a digital clock using PyQt5, which essentially displays the time in 24-hour format. We will be focussing on making a GUI that will show the current time in HH:MM:SS format by opening a window.

The following must be done in order to make a digital clock :

  1. Make a vertical arrangement
  2. Create a label to display the current time, place it in the layout, and centre it.
  3. Make a QTimer object.
  4. Include an action such that the QTimer object's action function is invoked once every 1 second.
  5. Obtain the current time and display it using a label inside the action function.

Walking through the code :

Firstly, we will import all the libraries required by our program, that include QtWidgets, QtGui, QtCore and sys.

Next, we will create a new class to initialize our GUI window and start setting the geometry, adding fonts, creating labels, setting alignment and displaying widgets.

Next, we will create the layout of our window. Then, we will create a timer clock timer, add a functionality to it for getting updated after every second and call the function linked to it.

Next, we will create a function that contains the present time to be displayed, the time label to be updated and the text display field.

Finally, we will create the application and the instance of the window. Then, the instance created will be displayed and the program will be called to execute and start the application.

Complete code :

Output:

The output of the above mentioned code is attached below in the for of screenshot,

Digital clock using PyQt5 in Python




Latest Courses