Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
843 views
in Technique[技术] by (71.8m points)

moving ellipses using addEllipse in python using PyQt5

i am working in a project where i should draw moving balls using PyQt5. So i tried to move the coordinates(x,y) of addElipse but it didnt want to work.

This is my Code:

import numpy as np
from PyQt5 import QtWidgets, QtCore
from PyQt5.QtCore import QPropertyAnimation, QRect
from PyQt5.QtGui import QPen, QBrush, QColor
from PyQt5.QtWidgets import QGraphicsScene

from mainwindow import Ui_MainWindow


class View(QtWidgets.QMainWindow, Ui_MainWindow):
    startSimulationSignal = QtCore.pyqtSignal()

    def __init__(self):
        super(View, self).__init__()
        self.setupUi(self)
        self.setWindowTitle('Simulation')
        self.scene = QGraphicsScene()
        self.graphicsView.setScene(self.scene)
        self.pen = QPen()
        self.blue_brush = QBrush(QColor(0, 0, 255))
        self._green_brush = QBrush(QColor(154, 205, 50))
        self.red_brush = QBrush(QColor(178, 34, 34))
        self.x = 10 * np.random.random_sample()
        self.y = 10 * np.random.random_sample()
        self.vx = 10 * np.random.random_sample() / 10
        self.vy = 10 * np.random.random_sample() / 10
        self.population = 100

        self.moving()
        self.drawing()
        self.collide()
        self.doanimation()

    def moving(self):
        if self.x < 1 or self.x >= 10:
            self.vx *= -1
        if self.y < 0 or self.y >= 10:
            self.vy *= -1
        self.x += int(self.vx)
        self.y += int(self.vy)

    def drawing(self):
        # self.population = 100
        for i in range(self.population):
            self.scene.addEllipse(i * 30, self.x, 30, self.y, self.pen, self.blue_brush)
            self.stat = 'not sick'

    def collide(self):
        for i in range(self.population):
            self.dx = self.x + self.vx
            self.dy = self.y + self.vy
            self.scene.addEllipse(i * 50, self.dx, 30, self.dy, self.pen, self._green_brush)

    def doanimation(self):
        self.anim = QPropertyAnimation(self.scene)  # ,b="geometry")
        self.anim.setStartValue(QRect(0, 0, 100, 30))
        self.anim.setEndValue(QRect(250, 250, 100, 30))
        self.anim.start()

mainwindow.py

from PyQt5 import QtCore, QtWidgets
from pyqtgraph import PlotWidget


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(1456, 835)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(950, 580, 93, 28))
        self.pushButton.setObjectName("pushButton")
        self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_2.setGeometry(QtCore.QRect(1120, 580, 93, 28))
        self.pushButton_2.setObjectName("pushButton_2")
        self.verticalLayoutWidget_2 = QtWidgets.QWidget(self.centralwidget)
        self.verticalLayoutWidget_2.setGeometry(QtCore.QRect(90, 100, 361, 231))
        self.verticalLayoutWidget_2.setObjectName("verticalLayoutWidget_2")
        self.verticalLayout_2 = QtWidgets.QVBoxLayout(self.verticalLayoutWidget_2)
        self.verticalLayout_2.setContentsMargins(0, 0, 0, 0)
        self.verticalLayout_2.setObjectName("verticalLayout_2")
        self.verticalLayoutWidget_3 = QtWidgets.QWidget(self.centralwidget)
        self.verticalLayoutWidget_3.setGeometry(QtCore.QRect(30, 350, 631, 431))
        self.verticalLayoutWidget_3.setObjectName("verticalLayoutWidget_3")
        self.verticalLayout_3 = QtWidgets.QVBoxLayout(self.verticalLayoutWidget_3)
        self.verticalLayout_3.setContentsMargins(0, 0, 0, 0)
        self.verticalLayout_3.setObjectName("verticalLayout_3")
        self.widget = QtWidgets.QWidget(self.centralwidget)
        self.widget.setGeometry(QtCore.QRect(750, 530, 120, 80))
        self.widget.setObjectName("widget")
        self.graphWidget = PlotWidget(self.centralwidget)
        self.graphWidget.setGeometry(QtCore.QRect(720, 40, 491, 381))
        self.graphWidget.setObjectName("graphWidget")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 1456, 26))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)
        self.graphicsView = QtWidgets.QGraphicsView(self.centralwidget)
        self.graphicsView.setGeometry(QtCore.QRect(10, 40, 601, 411))
        self.graphicsView.setObjectName("graphicsView")
        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.pushButton.setText(_translate("MainWindow", "Start"))
        self.pushButton_2.setText(_translate("MainWindow", "Pause"))
question from:https://stackoverflow.com/questions/66068508/moving-ellipses-using-addellipse-in-python-using-pyqt5

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The solution is simple: create a method that allows moving the items with a QVariantAnimation where the valueChanged signal updates the item's position. And then use the finished signal to recalculate the new position:

import random

from PyQt5.QtCore import QAbstractAnimation, QVariantAnimation, QPointF, QRectF
from PyQt5.QtGui import QPen, QBrush, QColor
from PyQt5.QtWidgets import (
    QApplication,
    QGraphicsEllipseItem,
    QGraphicsScene,
    QMainWindow,
)

from mainwindow import Ui_MainWindow


class Ball(QGraphicsEllipseItem):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.anim = QVariantAnimation()
        self.setRect(QRectF(-10, -10, 20, 20))
        self.setPen(QPen())

    def move_to(self, pos, duration=1000):
        if self.anim.state() == QAbstractAnimation.Running:
            self.anim.stop()
        self.anim.setStartValue(self.pos())
        self.anim.setEndValue(pos)
        self.anim.valueChanged.connect(self.setPos)
        self.anim.setDuration(duration)
        self.anim.start()


class View(QMainWindow, Ui_MainWindow):
    def __init__(self):
        super(View, self).__init__()
        self.setupUi(self)
        self.setWindowTitle("Simulation")
        self.scene = QGraphicsScene()
        self.graphicsView.setScene(self.scene)

        self.number_of_balls = 10

        self.start()

    def start(self):
        for i in range(self.number_of_balls):
            ball = Ball()
            color = random.choice(
                [QColor(0, 0, 255), QColor(154, 205, 50), QColor(178, 34, 34)]
            )
            ball.setBrush(QBrush(color))
            ball.anim.finished.connect(lambda item=ball: self.move_random(item))
            self.scene.addItem(ball)
            self.move_random(ball)

    def move_random(self, item):
        pos = QPointF(*random.sample(range(-100, 100), 2))
        item.move_to(pos)


if __name__ == "__main__":
    app = QApplication([])
    view = View()
    view.show()

    app.exec_()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...