Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在PyQt5中设置点在屏幕上移动的动画_Python_Python 3.x_Pyqt_Pyqt5 - Fatal编程技术网

Python 如何在PyQt5中设置点在屏幕上移动的动画

Python 如何在PyQt5中设置点在屏幕上移动的动画,python,python-3.x,pyqt,pyqt5,Python,Python 3.x,Pyqt,Pyqt5,我已经搜索了一段时间,没有得到我想要的结果。然而,我看到了它以及我看到的其他页面,它们描述了如何使用事件更新GUI。相反,我不想使用任何事件,如鼠标单击或按下任何键。我想给出.txt文件中的(x,y)-对应关系,并看到它根据给定值移动。对于问题的解决方案,只需知道: 可以通过更新(或重绘)间接调用重绘 若要在X特性变化时获得动画,则必须在两个连续点之间外推该特性,并且在Qt中,可以使用QXAnimation作为QPropertyAnimation 您能更详细地解释一下您想要什么吗?在链接示例

我已经搜索了一段时间,没有得到我想要的结果。然而,我看到了它以及我看到的其他页面,它们描述了如何使用事件更新GUI。相反,我不想使用任何事件,如鼠标单击或按下任何键。我想给出.txt文件中的(x,y)-对应关系,并看到它根据给定值移动。

对于问题的解决方案,只需知道:

  • 可以通过更新(或重绘)间接调用重绘
  • 若要在X特性变化时获得动画,则必须在两个连续点之间外推该特性,并且在Qt中,可以使用QXAnimation作为QPropertyAnimation

您能更详细地解释一下您想要什么吗?在链接示例中,绘制了一条线,将鼠标点击按下的点与拖动鼠标的点连接起来。在你的情况下,应该画什么?还显示了中的信息。txt@eyllanesc我想要一个简单的点,在平面上从一个坐标移动到另一个坐标。txt文件给出了类似(1,1),(2,1),(2,2)的坐标。是否可以以相同的方式设置两个点?像蛇游戏?
from PyQt5 import QtCore, QtGui, QtWidgets


class Widget(QtWidgets.QWidget):
    pointChanged = QtCore.pyqtSignal(QtCore.QPoint)

    def __init__(self, parent=None):
        super().__init__(parent)

        self._point = QtCore.QPoint(0, 0)

        self._animation = QtCore.QPropertyAnimation(
            self,
            duration=1000,
            propertyName=b"point",
            targetObject=self,
            startValue=QtCore.QPoint(0, 0),
            endValue=QtCore.QPoint(0, 0),
            finished=self.calculate_next_point,
        )

        points = (
            QtCore.QPoint(10, 100),
            QtCore.QPoint(400, 300),
            QtCore.QPoint(210, 500),
            QtCore.QPoint(500, 210),
            QtCore.QPoint(500, 200),
            QtCore.QPoint(150, 0),
            QtCore.QPoint(150, 100),
            QtCore.QPoint(50, 300),
            QtCore.QPoint(0, 500),
            QtCore.QPoint(110, 350),
        )
        self._points_iter = iter(points)
        self.calculate_next_point()

    def _update_end_point(self, p):
        s = self._animation.endValue()
        self._animation.setStartValue(s)
        self._animation.setEndValue(p)
        self._animation.start()

    def calculate_next_point(self):
        try:
            p = next(self._points_iter)
        except StopIteration:
            print("finished")
        else:
            self._update_end_point(p)

    @QtCore.pyqtProperty(QtCore.QPoint, notify=pointChanged)
    def point(self):
        return self._point

    @point.setter
    def point(self, p):
        self._point = p
        self.pointChanged.emit(p)
        self.update()

    def paintEvent(self, event):
        radius = 10

        painter = QtGui.QPainter(self)
        painter.setRenderHint(QtGui.QPainter.Antialiasing)

        rect = QtCore.QRect(0, 0, 2 * radius, 2 * radius)
        rect.moveCenter(self.point)

        painter.setBrush(QtGui.QBrush(QtGui.QColor("salmon")))
        painter.drawEllipse(rect)


def main():
    import sys

    app = QtWidgets.QApplication(sys.argv)

    w = Widget()
    w.show()

    sys.exit(app.exec_())


if __name__ == "__main__":
    main()