from PySide2.QtCore import QTimer def print_message(): print("Hello World") timer = QTimer() timer.timeout.connect(print_message) timer.start(1000)
from PySide2.QtCore import QTimer, QTime from PySide2.QtWidgets import QApplication, QLabel, QVBoxLayout, QWidget def update_time(): current_time = QTime.currentTime().toString('hh:mm:ss') label.setText(current_time) app = QApplication([]) window = QWidget() layout = QVBoxLayout(window) label = QLabel("00:00:00") layout.addWidget(label) timer = QTimer() timer.timeout.connect(update_time) timer.start(1000) window.show() app.exec_()Explanation: The code imports the QTimer and QTime classes from PyQt5/PySide2.QtCore library and the QApplication, QLabel, QVBoxLayout, and QWidget classes from PyQt5/PySide2.QtWidgets library. The `update_time` function is defined, which retrieves the current time using the `QTime` class and updates the label with the current time. A QApplication instance is created, and a window and a label are added to it using a QVBoxLayout layout. A QTimer instance is created and its `timeout` signal is connected to the `update_time` function. The `start` method is called to start the timer. The window is displayed using the `show` method, and the application event loop is started using `exec_` method. In conclusion, the `QTimer` class is a high-level interface for creating timers in PyQt5/PySide2.QtCore, which can be used to execute a piece of code or function at specific intervals.