PyQt5 – Create a digital clock
In this article we will see how to create a digital clock using PyQt5, this digital clock will basically tells the current time in the 24 hour format.
In order to create a digital clock we have to do the following:
- Create a vertical layout
- Create label to show the current time and put it in the layout and align it to the center.
- Create a QTimer object.
- Add action to the QTimer object such that after every 1sec action method get called.
- Inside the action method get the current time and show that time with the help of label.
Below is the implementation:
- The code starts by importing the required libraries.
- Then it creates a class called Window and initializes it with __init__() method.
- Next, the code sets the geometry of the main window to 100×100 pixels and 800×400 pixels respectively.
- It then creates a vertical layout object which is used for arranging widgets in order vertically on the screen.
- The next step is creating font object using QFont class from PyQt5 library followed by creating a label object using QLabel class from PyQt5 library.
- Finally, the code adds a label to the layout and sets its alignment to Qt’s align center option before setting font of label to Arial font with a size 120 points bold style.
- The code is a simple example of how to create a window and show it.