from PySide2.QtCore import QTimer def my_function(): print("Timer expired") # create a timer that will expire after 5 seconds timer = QTimer() timer.setSingleShot(True) timer.timeout.connect(my_function) timer.start(5000)
from PySide2.QtCore import QTimer, QCoreApplication class MyObject(): def __init__(self): self.timer = QTimer() self.timer.setSingleShot(True) self.timer.timeout.connect(self.my_function) self.timer.start(5000) def my_function(self): print("Timer expired") QCoreApplication.quit() # create an instance of the MyObject class obj = MyObject() # start the event loop app = QCoreApplication() app.exec_()Here, a QTimer is created as a member of a class. The timer will trigger only once after 5 seconds, and calls the my_function method of the class which prints "Timer expired" to the console and quits the application's event loop. In both examples, the QTimer class from PySide2.QtCore package is used along with the setSingleShot method to create a timer that will only trigger once.