2015-09-03 28 views
0
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() - 找不到參考,這應該可以解決,但不和,當我運行它沒有任何反應。然後我嘗試QtCore.QTimer.singleShot(700, func)而不是上面的定時器,它完美地工作,但只執行一次(因爲它應該)。我試圖製作一個執行很多次計時器的事情都失敗了。請幫忙。PyQt4的 - timer.timeout.connect() - 無法找到引用

回答

2

你犯了一個非常普遍的錯誤。沒有任何鏈接指向您的timer,因此在chaise函數結束後它將被刪除。將timer替換爲self.timer

+0

非常感謝! – vixenn

+0

如果我有一個函數可以生成要連接的元素,那我該怎麼做?在函數返回後,我只會將它作爲實例變量保存,因爲在函數中我無法決定將哪個實例變量賦值給它。看到這裏: http://stackoverflow.com/questions/37534093/how-to-react-to-a-button-click-in-pyqt5?noredirect=1#comment62559489_37534093 – findusl