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

问题描述 投票:0回答:1

我想根据15 FPS的帧速率更新Qtimer-所以我的def update():每0,06 s接收到一个信号。你能帮助我吗?我在下面附加了一个代码示例,其中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()
python pyqt pyqt5 qtimer
1个回答
0
投票

您有以下错误:

  • [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_()
© www.soinside.com 2019 - 2024. All rights reserved.