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