...

/

Creating a Clock GUI

Creating a Clock GUI

Learn how we can use the QDateTime module to create a clock GUI.

We'll cover the following...

The clock GUI

Additionally, PyQt6 has the QDate and QTime classes for working with dates and times. Functions are available for working with dates and times in the QDateTime class. The methods for managing time-related aspects are included in all three of these classes.

Firstly, we start by importing the required modules.

Press + to interact
import sys
from PyQt6.QtWidgets import (QApplication, QWidget, QLabel,
QVBoxLayout)
from PyQt6.QtCore import Qt, QDate, QTime, QTimer

Then we create the DisplayTime class where each second, the timeout() signal is broadcast. The currentDate() and currentTime() methods of the getDateTime() function are used to acquire the values to get the current date and time. Following their return, the current_date and current_time are set. The time employs a series of characters to generate a format string that displays hours (hh), minutes (mm), seconds (ss), and AM or PM (AP) while the date uses the standard Qt.DateFormat.ISODate format. The labels for the layout that will show the date and time are then created, stylized, and applied.