web-dev-qa-db-fra.com

PyQt4 - création d'une minuterie

Je suis désolé pour la question, mais j'ai lu un tas de choses et il semble que je ne sais pas comment faire une minuterie. Je poste donc mon 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())

J'essaie de faire bouger l'objet vers son objectif toutes les 200 ms. J'ai essayé sans moi ça me donne les mêmes erreurs:

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'

Je n'ai aucune idée de comment connecter la minuterie à une fonction avec des arguments. Je pensais que je n'utilisais pas correctement l'argument SLOT mais cela m'a donné ces mystères. Je suppose que tout va mal. J'apprécierais de l'aide :)

9
vixenn

Utilisez de nouveaux signaux de style, ils sont plus faciles à comprendre.

Échange -

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

Avec -

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

Un exemple simple mais complet d'une minuterie de travail -

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_()
17
Tim Wakeham