问题描述
我想根据 15 FPS 的帧速率更新 Qtimer - 所以我的 def update(): 每 0.06 秒接收一个信号.你能帮助我吗?我在下面附上了一个代码示例,其中我的 setInterval 输入是 1/15,但我不知道这是否可行.谢谢.
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()
推荐答案
您有以下错误:
setInterval()
以毫秒为单位接收时间,因此您必须将其更改为timer.setInterval(1000/fps)
.
setInterval()
receives the time in milliseconds, so you must change it totimer.setInterval(1000/fps)
.
像许多 Qt 组件一样,QTimer 需要您创建 QXApplication 并启动事件循环,在这种情况下,一个 QCoreApplication 就足够了.
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_()
这篇关于如何设置 PyQt5 Qtimer 以指定的时间间隔更新?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!