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

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.

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

If i have a function that generates the element that i want to connect to, how can i do that? I will only save it as an instance variable after the function returned it, as in the function i can't decide to which instance variable to assign it. See here: stackoverflow.com/questions/37534093/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.