如何设置PyQt5 Qtimer在指定的时间间隔更新?

How to set PyQt5 Qtimer to update in specified interval?

我想根据 15 FPS 的帧率更新 Qtimer - 所以我的 def update(): 每 0.06 秒接收一次信号。你能帮助我吗?我在下面附上了一个代码示例,其中我的 setInterval 输入是 1/15,但我不知道这是否可行。谢谢

from PyQt5 import QtCore

def update():
    print('hey')

fps = 15
timer = QtCore.QTimer()
timer.timeout.connect(update)
timer.setInterval(1/fps)
timer.start()

您有以下错误:

  • setInterval()接收的是毫秒时间,所以必须改成timer.setInterval(1000/fps).

  • 像许多 Qt 组件一样,QTimer 需要您创建 QXApplication 并启动事件循环,在这种情况下,一个 QCoreApplication 就足够了。

import sys

from PyQt5 import QtCore


def update():
    print("hey")


if __name__ == "__main__":

    app = QtCore.QCoreApplication(sys.argv)

    fps = 15
    timer = QtCore.QTimer()
    timer.timeout.connect(update)
    timer.setInterval(1000 / fps)
    timer.start()

    app.exec_()