from PySide2.QtCore import QTimer def do_something(): print("Hello world!") # Create a QTimer timer = QTimer() # Call the do_something function after 1000 ms (1 second) timer.singleShot(1000, do_something) # Start the timer timer.start()
from PySide2.QtCore import QTimer class MyWidget(QWidget): def __init__(self): super().__init__() # Set up a QTimer to update every 500ms (0.5 seconds) self.timer = QTimer() self.timer.setInterval(500) self.timer.timeout.connect(self.update_text) def update_text(self): # This function will be called every 500ms (0.5 seconds) self.label.setText("Hello world!") # Create a new QApplication app = QApplication(sys.argv) # Create a new instance of our widget class widget = MyWidget() # Set up a QLabel that will be updated using a QTimer widget.label = QLabel("") # Start the QTimer widget.timer.start() # Show the widget widget.show() # Run the application sys.exit(app.exec_())In this example, we define a class called `MyWidget` that inherits from the `QWidget` class. Inside the `MyWidget` class, we set up a QTimer object called `timer` that will fire every 500ms and call the `update_text` function. The `update_text` function will update a QLabel with the text "Hello world!". We then create a new QApplication object and a new instance of our `MyWidget` class. We set up a QLabel that we will update using the QTimer and start the timer. Finally, we show the widget and run the application using `app.exec_()`.