PyQt4 - create a timer

I apologize for the question, but I read a bunch of things, and it seems that I do not understand how to make a timer. So I submit my code:

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) while not path.empty(): aim = path.get_nowait() func = functools.partial(self.move_towards, aim) timer = QtCore.QTimer() QtCore.QTimer.connect(timer, QtCore.SIGNAL("timeout()"), self, QtCore.SLOT("func()")) timer.start(200) def move_towards(self, goal): self.setPos(goal.first(), goal.second()) 

I try to make an object move toward its target every 200 ms. I tried without myself, it gives me the same errors:

 QObject.connect(QObject, SIGNAL(), QObject, SLOT(), Qt.ConnectionType=Qt.AutoConnection): argument 3 has unexpected type 'bytes' QObject.connect(QObject, SIGNAL(), callable, Qt.ConnectionType=Qt.AutoConnection): argument 3 has unexpected type 'bytes' QObject.connect(QObject, SIGNAL(), SLOT(), Qt.ConnectionType=Qt.AutoConnection): argument 2 has unexpected type 'bytes' 

I have no idea how to connect a timer to a function with arguments. I thought I was not using the SLOT argument correctly, but he gave me these puzzles. I suppose all this is wrong. I would appreciate help :)

+6
source share
1 answer

Use new style cues, they are easier to understand.

Swap -

 QtCore.QTimer.connect(timer, QtCore.SIGNAL("timeout()"), self, QtCore.SLOT("func()")) 

WITH -

 timer.timeout.connect(self.move_towards) # assuming that move_towards is the handler 

A simple but complete example of a working timer is

 import sys from PyQt4.QtCore import QTimer from PyQt4.QtGui import QApplication app = QApplication(sys.argv) app.setQuitOnLastWindowClosed(False) def tick(): print 'tick' timer = QTimer() timer.timeout.connect(tick) timer.start(1000) # run event loop so python doesn't exit app.exec_() 
+10
source

All Articles