python - PyQt4 - 创建一个计时器

标签 python timer pyqt

对于这个问题,我很抱歉,但我已经阅读了很多东西,但似乎我不知道如何制作计时器。所以我发布了我的代码:

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

我试图让物体每 200 毫秒向其目标移动一次。 我没有 self 尝试它给了我同样的错误:

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'

我不知道如何将计时器连接到带参数的函数。 我认为我没有正确使用 SLOT 参数,但它给了我那些神秘的赌注。我想这都是错误的。我会很感激一些帮助:)

最佳答案

使用新样式的信号,它们更容易理解。

交换-

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

与 -

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

一个简单但完整的工作计时器示例 -

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

关于python - PyQt4 - 创建一个计时器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32362340/

相关文章:

python - 是否可以使用 wxPython 将菜单对齐到菜单栏的右侧?

java - 如何使用定时器类

python - QTreeView 与 sqlalchemy

qt - QLabel setText 在运行其他方法之前不立即显示文本

python - PyQt:如何在 QStackedWidget 对象内移动小部件?

python - 我需要一个 Python 函数,当给定所需的字符概率时,它将输出 4 个不同字符的随机字符串

python - 寻找一种更快的方法在包含另一列行中的字典值的数据框中创建新列

python - Scrapy:下一个按钮使用 javascript

javascript - 每半小时模拟一次点击

timer - JEE6 计时器在重新部署中的持久性