from PySide2.QtCore import QTimer def print_message(): print("Timer triggered.") # create a QTimer object timer = QTimer() # set the interval to 1000 ms (1 second) timer.setInterval(1000) # connect the timeout signal to a function timer.timeout.connect(print_message) # start the timer timer.start()
from PySide2.QtCore import QTimer from PySide2.QtWidgets import QLabel, QWidget, QVBoxLayout, QApplication class MyWindow(QWidget): def __init__(self): super().__init__() self.label = QLabel("10") # create a QTimer object self.timer = QTimer() # set the interval to 500 ms self.timer.setInterval(500) # connect the timeout signal to the update_label function self.timer.timeout.connect(self.update_label) # start the timer self.timer.start() # set up the layout layout = QVBoxLayout() layout.addWidget(self.label) self.setLayout(layout) def update_label(self): time_left = int(self.label.text()) - 1 if time_left <= 0: self.timer.stop() self.label.setText("Time's up!") else: self.label.setText(str(time_left)) # create the application app = QApplication([]) # create the widget and show it window = MyWindow() window.show() # run the event loop app.exec_()In conclusion, PySide2.QtCore is a package library that provides QTimer to enable the creation of timer-based events. Examples given illustrate how to create timers with set intervals that prints or updates GUI text labels.