Displaying a 2D graphical image
Final Result
How to do it?
class MyForm(QDialog):
def __init__(self):
super().__init__()
self.ui = Ui_Dialog()
self.ui.setupUi(self)
self.scene = QGraphicsScene(self)
pixmap = QtGui.QPixmap()
pixmap.load("scene.jpg")
item = QGraphicsPixmapItem(pixmap)
self.scene.addItem(item)
self.ui.graphicsView.setScene(self.scene)
if __name__=="__main__":
app = QApplication(sys.argv)
myapp = MyForm()
myapp.show()
sys.exit(app.exec_())
Making a ball move down on the click of a button
Final Result
How to do it?
class MyForm(QDialog):
def __init__(self):
super().__init__()
self.ui = Ui_Dialog()
self.ui.setupUi(self)
self.ui.pushButton.clicked.connect(self.startAnimation)
self.show()
def startAnimation(self):
self.anim = QPropertyAnimation(self.ui.label,b"geometry")
self.anim.setDuration(10000)
self.anim.setStartValue(QRect(160,70,80,80))
self.anim.setEndValue(QRect(160,70,220,220))
self.anim.start()
if __name__ == "__main__":
app = QApplication(sys.argv)
w = MyForm()
w.show()
sys.exit(app.exec_())
Making a bouncing ball
Final Result
How to do it?
class MyForm(QDialog):
def __init__(self):
super().__init__()
self.ui =Ui_Dialog()
self.ui.setupUi(self)
self.ui.pushButton.clicked.connect(self.startAnimation)
self.show()
def startAnimation(self):
self.anim = QPropertyAnimation(self.ui.label,b"geometry")
self.anim.setDuration(10000)
self.anim.setKeyValueAt(0, QRect(0,0,100,80))
self.anim.setKeyValueAt(0.5,QRect(160,160,200,180))
self.anim.setKeyValueAt(1,QRect(400,0,100,80))
self.anim.start()
if __name__=="__main__":
app = QApplication(sys.argv)
w = MyForm()
w.show()
sys.exit(app.exec_())
Making a ball animate as per the specified curve
Final Result
How to do it?
class MyForm(QDialog):
def __init__(self):
super().__init__()
self.ui = Ui_Dialog()
self.ui.setupUi(self)
self.ui.pushButton.clicked.connect(self.startAnimation)
self.path = QPainterPath()
self.path.moveTo(30, 30)
self.path.cubicTo(30,30,80,180,180,170)
self.ui.label.pos = QPointF(20,20)
self.show()
def paintEvent(self, e):
qp = QPainter()
qp.begin(self)
qp.drawPath(self.path)
qp.end()
def startAnimation(self):
self.anim = QPropertyAnimation(self.ui.label,b"pos")
self.anim.setDuration(4000)
self.anim.setStartValue(QPointF(20,20))
positionValues = [n/80 for n in range(0,50)]
for i in positionValues:
self.anim.setKeyValueAt(i,self.path.pointAtPercent(i))
self.anim.setEndValue(QPointF(160,150))
self.anim.start()
if __name__=="__main__":
app = QApplication(sys.argv)
w = MyForm()
w.show()
sys.exit(app.exec_())