PyQt4-timer.timeout.connect()-找不到参考 [英] PyQt4 - timer.timeout.connect() - cannot find reference
问题描述
from PyQt4 import QtGui, QtCore
from code.pair import Pair
from code.breadth_first_search import breadth_first_search
import functools
class Ghosts(QtGui.QGraphicsPixmapItem):
def __init__(self, name):
super(Ghosts, self).__init__()
self.set_image(name)
def chase(self, goal):
pos = Pair(self.x(), self.y())
path = breadth_first_search(pos, goal)
func = functools.partial(self.move_towards, path)
timer = QtCore.QTimer()
timer.timeout.connect(func)
timer.start(700)
def move_towards(self, path):
print("in")
if path.empty():
return
goal = path.get_nowait()
self.setPos(goal.first(), goal.second())
当我键入它时,它告诉我 timer.timeout.connect()
-找不到引用, ld解决,但是没有解决,运行时什么也没发生。然后,我尝试使用 QtCore.QTimer.singleShot(700,func)
而不是上面的计时器,它可以正常运行,但只执行一次(应该如此)。我试图使计时器执行多次的所有操作均失败。请帮忙。
When I type this it tells me timer.timeout.connect()
- cannot find reference, this should resolve but doesn't and nothing happens when I run it. Then I try QtCore.QTimer.singleShot(700, func)
instead of the timer above and it works perfecly but executes only once (as it should). Everything I tried to make a timer that executes many times fails. Please help.
推荐答案
您犯了一个非常常见的错误。没有内容链接到您的计时器
,因此在 chaise
函数结束后,该链接将被删除。将 timer
替换为 self.timer
。
You made a very common mistake. Nothing holds a link to your timer
, so it gets deleted after chaise
function ends. Replace timer
with self.timer
.
这篇关于PyQt4-timer.timeout.connect()-找不到参考的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!