python - PySide6 中 QTimer() 函数的问题该函数不循环

标签 python pyqt qtimer pyside6

我有这个 PySide 应用程序,我想每 1 秒运行一次函数 pp,但是当我运行该应用程序时,它只运行了 1 次。

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())

控制台输出:

LOL
True

我在 Qt documentation 中搜索但什么也没发现

最佳答案

您的连接错误。将 self.timer.timeout.connect(self.pp()) 更改为 self.timer.timeout.connect(self.pp),因为您想要连接到函数,而不是其输出。

关于python - PySide6 中 QTimer() 函数的问题该函数不循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70118356/

相关文章:

python - 停止 QTimer.singleShot() 计时器

c++ - 在 QTimer 重置时触发不同的事件

python - 在 matplotlib colorbar 中对齐刻度标签

Chromebook 上的 Python

python - Python 可以实现快速蜡烛图吗

python - 如何为 Python 2.7 安装 PyQT4?

python - setUpClass可以在unnitest框架中扩展吗?

python - AppEngine GeoPt 数据上传

python - 如何在新的 virtualenv 上安装 PyQt5 并在 IDLE 上工作

c++ - 如何计算传感器从其 3D 点位置集覆盖的总距离?