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

Python 如何将PyQt5 Qtimer设置为在指定的时间间隔内更新?,python,pyqt,pyqt5,qtimer,Python,Pyqt,Pyqt5,Qtimer,我想根据每秒15帧的帧率更新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.setInterva

我想根据每秒15帧的帧率更新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就足够了

导入系统 从PyQt5导入QtCore def update(): 打印(“嘿”) 如果名称=“\uuuuu main\uuuuuuuu”: app=QtCore.QCoreApplication(sys.argv) fps=15 计时器=QtCore.QTimer() timer.timeout.connect(更新) 定时器设置间隔(1000/fps) timer.start() app.exec()
除了间隔计算(QTimer间隔以毫秒为单位设置,因此应为1000/15),这是正确的方法。