Displaying a 2D graphical image

Final Result

C11:Implementing Animation

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

C11:Implementing Animation

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

C11:Implementing Animation

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

C11:Implementing Animation

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

 

相关文章:

  • 2021-11-15
  • 2021-11-15
  • 2021-11-15
  • 2021-11-15
  • 2021-05-18
  • 2021-07-26
  • 2021-10-27
  • 2021-06-09
猜你喜欢
  • 2021-08-13
  • 2022-12-23
  • 2022-12-23
  • 2021-11-15
  • 2021-11-15
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案