How to set PyQt5 Qtimer to update in specified interval?

a 夏天 提交于 2021-02-10 14:18:19

问题


I would like to update the Qtimer according to a framerate of 15 FPS - so my def update(): recieves a signal every 0,06 s. Can you help me? I have attached a code example below where my setInterval input is 1/15, but I dont know if that is the way to go. Thanks.

from PyQt5 import QtCore

def update():
    print('hey')

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

回答1:


You have the following errors:

  • setInterval() receives the time in milliseconds, so you must change it to timer.setInterval(1000/fps).

  • Like many Qt components, the QTimer needs you to create QXApplication and start the event loop, in this case a QCoreApplication is enough.

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


来源:https://stackoverflow.com/questions/59094207/how-to-set-pyqt5-qtimer-to-update-in-specified-interval

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!