Creating a PyQt LCD Clock using QTime

Posted by


In this tutorial, we will create a PyQt application that displays a digital clock using the QLCDNumber widget and the QTime class. We will first cover the basics of PyQt and then dive into creating a simple LCD clock that updates every second.

PyQt is a set of Python bindings for the Qt application framework. It allows you to create desktop applications using the Qt GUI toolkit. PyQt provides a set of Python modules that make it easy to work with Qt widgets, layouts, and signals/slots mechanism.

To get started, make sure you have PyQt5 installed on your system. You can install it using pip:

pip install PyQt5

Now, let’s create a PyQt application that displays a digital clock using the QLCDNumber widget and the QTime class.

import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QLCDNumber
from PyQt5.QtCore import QTimer, QTime

class ClockApp(QMainWindow):
    def __init__(self):
        super().__init__()

        self.initUI()

    def initUI(self):
        self.setGeometry(100, 100, 200, 100)
        self.setWindowTitle('PyQt LCD Clock')

        self.lcd = QLCDNumber(self)
        self.lcd.setDigitCount(8) # Set the number of digits to display
        self.lcd.setSegmentStyle(QLCDNumber.Flat) # Set the segment style

        timer = QTimer(self)
        timer.timeout.connect(self.updateTime)
        timer.start(1000) # Update every second

    def updateTime(self):
        time = QTime.currentTime()
        text = time.toString('hh:mm:ss')
        self.lcd.display(text)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    clock = ClockApp()
    clock.show()
    sys.exit(app.exec_())

In this code, we create a subclass of QMainWindow called ClockApp. In the initUI() method, we create an instance of the QLCDNumber widget and set its properties to display a digital clock. We also create a QTimer that updates the clock every second by calling the updateTime() method.

In the updateTime() method, we use the QTime class to get the current time and format it as ‘hh:mm:ss’. We then display the formatted time on the QLCDNumber widget using the display() method.

To run the application, save the code to a file (e.g., clock.py) and run it using Python:

python clock.py

You should see a window displaying a digital clock that updates every second. You can customize the appearance of the clock by changing the properties of the QLCDNumber widget, such as the digit count and segment style.

This tutorial covers the basics of creating a PyQt application that displays a digital clock using the QLCDNumber widget and the QTime class. You can further enhance the application by adding features such as setting alarms, displaying different time zones, or creating a countdown timer. PyQt provides a wide range of widgets and classes that you can use to create powerful desktop applications with a modern and intuitive user interface.

0 0 votes
Article Rating

Leave a Reply

2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@thelonespeaker
1 hour ago

Bloody brilliant

@tyronepearce4433
1 hour ago

How would you say make the time GMT-2 from your current time ?

2
0
Would love your thoughts, please comment.x
()
x