w3resource

Python PyQt time display

Python PyQt Connecting Signals to Slots: Exercise-12 with Solution

Write a Python program that creates an application with a window that displays the current time. Use the PyQt module.

From doc.qt.io:

QApplication Class: The QApplication class manages the GUI application's control flow and main settings.

QWidget Class: The QWidget class is the base class of all user interface objects.

QLabel Class: The QLabel widget provides a text or image display.

QVBoxLayout Class: The QVBoxLayout class lines up widgets vertically.

QTimer Class: The QTimer class provides repetitive and single-shot timers.

QTime Class: The QTime class provides clock time functions.

Sample Solution:

Python Code:

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QVBoxLayout
from PyQt5.QtCore import QTimer, QTime

class TimeDisplayApp(QWidget):
    def __init__(self):
        super().__init__()

        self.setWindowTitle("Time Display")
        self.setGeometry(100, 100, 300, 100)  # (x, y, width, height)

        layout = QVBoxLayout()

        # Create a label for displaying the time
        self.time_label = QLabel(self)
        layout.addWidget(self.time_label)

        self.setLayout(layout)

        # Create a QTimer that updates the time label every second
        self.timer = QTimer(self)
        self.timer.timeout.connect(self.update_time)
        self.timer.start(1000)  # Update every 1000 milliseconds (1 second)

    def update_time(self):
        # Get the current time and set it on the label
        current_time = QTime.currentTime().toString("hh:mm:ss")
        self.time_label.setText(f"Current Time: {current_time}")

def main():
    app = QApplication(sys.argv)
    window = TimeDisplayApp()
    window.show()
    sys.exit(app.exec_())

if __name__ == "__main__":
    main()

Explanation:

In the exercise above -

  • Import the necessary modules from PyQt5.
  • Create a custom "QWidget" class named "TimeDisplayApp" that builds the main application window.
  • Inside the window, there's a label (self.time_label) for displaying the current time.
  • Set up a QTimer (self.timer) that triggers the update_time slot function every second (1000 milliseconds).
  • In the "update_time()" slot function, we get the current time and set it on the label. This function runs every second, so the label updates with the current time automatically.
  • In the main function, we create the PyQt application, instantiate the TimeDisplayApp class, and show the main window.

Output:

PyQt: Python PyQt time display. Part-1

Flowchart:

Flowchart: Python PyQt time display.

Python Code Editor:


Previous: Python PyQt close window confirmation.
Next: Python PyQt checkbox example.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://198.211.115.131/python-exercises/pyqt/python-pyqt-connecting-signals-to-slots-exercise-12.php