Search code examples
pythonpyqtqtimerpyside6

Problem with QTimer() function in PySide6 the function doesn't loop


I have this PySide app and I want to run the function pp every 1 second, but when I run the app it only ran for 1 time.

import sys
from PySide6.QtWidgets import QMainWindow, QApplication
from PySide6 import QTimer


class MainWindow(QMainWindow):
    def __init__(self):

        QMainWindow.__init__(self)
        ###
        self.timer = QTimer()
        self.timer.timeout.connect(self.pp())
        self.timer.start(1000)
        print(self.timer.isActive())

    def pp(self):
        print("LOL")


if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
    #app.show()
    sys.exit(app.exec())

The console outputs:

LOL
True

I searched in the Qt documentation but found nothing


Solution

  • Your connection is wrong. Change self.timer.timeout.connect(self.pp()) to self.timer.timeout.connect(self.pp), because you want to connect to the function, not its output.