一、pyqt5中动画的继承关系图

pyqt5中动画类的继承

二、关于QAbstractAnimation父类的认识

  • 1、主要作用

    • 继承此类, 实现一些自定义动画
    • 所有动画共享的功能
  • 2、功能作用

    • 循环操作
      • setLoopCount(count): 设置循环次数
      • currentLoop(): 当前循环
      • currentLoopTime(): 当前循环时间
    • 时间操作
      • duration(): 单次时长
      • totalDuration(): 动画总时长
      • currentTime(): 当前时长
    • 动画方向
      • setDirection(QAbstractAnimation.Forward/QAbstractAnimation.Backward)
    • 动画状态state()
      • QAbstractAnimation.Stopped: 动画停止
      • QAbstractAnimation.Paused: 动画暂停
      • QAbstractAnimation.Running: 动画运行

三、QPropertyAnimation属性动画的使用

主要用于实现某个属性值从 x 到 y 的动画变化

  • 1、定义动画的主要步骤

    • 创建一个动画, 并设置目标、属性
    • 设置属性值的开始、插值、结束
    • 动画时长
    • 启动动画
  • 2、构造函数使用方式

    • 1.QPropertyAnimation(parent: QObject = None)
      • 设置动画目标:setTargetObject(self, QObject)
      • 设置动画属性 (位置、大小等):setPropertyName(self, Union[QByteArray, bytes, bytearray])
    • 2.QPropertyAnimation(QObject, Union[QByteArray, bytes, bytearray], parent: QObject = None)
  • 3、常见的属性

    • geometry
    • pos
    • size
    • windowOpacity
  • 4、设置开始值和结束值

    • setStartValue(self, Any)
    • setEndValue(self, Any)
    • setKeyValueAt(self, float, Any)
    • setKeyValues(self, object)
  • 5、设置动画时长

    • setDuration(int mesc)
  • 6、启动动画

    • start()
  • 7、简单案例 (位置的)

    import sys
    from PyQt5.Qt import *
    

    class Window(QWidget):
    def init(self, *args, **kwargs):
    super().init(*args, **kwargs)
    self.setWindowTitle(‘动画’)
    self.resize(500, 500)
    self.move(400, 200)
    self.btn = QPushButton(self)
    self.init_ui()

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">init_ui</span><span class="hljs-params">(self)</span>:</span>
        self.btn.resize(<span class="hljs-number">100</span>, <span class="hljs-number">100</span>)
        self.btn.move(<span class="hljs-number">0</span>, <span class="hljs-number">0</span>)
        self.btn.setStyleSheet(<span class="hljs-string">'QPushButton{border: none; background: pink;}'</span>)
    
        <span class="hljs-comment"># 1.定义一个动画</span>
        animation = QPropertyAnimation(self)
        animation.setTargetObject(self.btn)
        animation.setPropertyName(<span class="hljs-string">b'pos'</span>)
        <span class="hljs-comment"># 使用另外一种构造函数方式创建</span>
        <span class="hljs-comment"># animation = QPropertyAnimation(self.btn, b'pos', self)</span>
    
        <span class="hljs-comment"># 2.设置属性值</span>
        animation.setStartValue(QPoint(<span class="hljs-number">0</span>, <span class="hljs-number">0</span>))
        animation.setEndValue(QPoint(<span class="hljs-number">400</span>, <span class="hljs-number">400</span>))
    
        <span class="hljs-comment"># 3.设置时长</span>
        animation.setDuration(<span class="hljs-number">3000</span>)
    
        <span class="hljs-comment"># 4.启动动画</span>
        animation.start()
    

    if name == main:
    app = QApplication(sys.argv)
    window = Window()
    window.show()
    sys.exit(app.exec_())

    复制代码
  • 8、使用插值的动画

    import sys
    from PyQt5.Qt import *
    

    class Window(QWidget):
    def init(self, *args, **kwargs):
    super().init(*args, **kwargs)
    self.setWindowTitle(‘使用插值’)
    self.resize(500, 500)
    self.move(400, 200)
    self.btn = QPushButton(self)
    self.init_ui()

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">init_ui</span><span class="hljs-params">(self)</span>:</span>
        self.btn.resize(<span class="hljs-number">50</span>, <span class="hljs-number">50</span>)
        self.btn.move(<span class="hljs-number">0</span>, <span class="hljs-number">0</span>)
        self.btn.setStyleSheet(<span class="hljs-string">'QPushButton{border: none; background: pink;}'</span>)
        
        <span class="hljs-comment"># 1.创建动画</span>
        animation = QPropertyAnimation(self.btn, <span class="hljs-string">b'pos'</span>, self)
        
        <span class="hljs-comment"># 2.定义动画插值</span>
        animation.setKeyValueAt(<span class="hljs-number">0</span>, QPoint(<span class="hljs-number">0</span>, <span class="hljs-number">0</span>))
        animation.setKeyValueAt(<span class="hljs-number">0.25</span>, QPoint(<span class="hljs-number">450</span>, <span class="hljs-number">0</span>))
        animation.setKeyValueAt(<span class="hljs-number">0.5</span>, QPoint(<span class="hljs-number">450</span>, <span class="hljs-number">450</span>))
        animation.setKeyValueAt(<span class="hljs-number">0.75</span>, QPoint(<span class="hljs-number">0</span>, <span class="hljs-number">450</span>))
        animation.setKeyValueAt(<span class="hljs-number">1</span>, QPoint(<span class="hljs-number">0</span>, <span class="hljs-number">0</span>))
        <span class="hljs-comment"># 3.动画时长</span>
        animation.setDuration(<span class="hljs-number">5000</span>)
        <span class="hljs-comment"># 4.启动动画</span>
        animation.start()
    

    if name == main:
    app = QApplication(sys.argv)
    window = Window()
    window.show()
    sys.exit(app.exec_())

    复制代码

四、QAnimationGroup动画组的使用

可以将一组动画, 同时播放或者按顺序播放

  • 1、使用的步骤

    • 根据上面的方式创建单独的动画 (但不启动)
    • 定义一个动画组
    • 将之前的动画添加到动画组中
    • 启动动画组
  • 2、动画运行几种状态

    • 并行动画QParallelAnimationGroup
    • 串行动画QSequentialAnimationGroup
  • 3、一个动画组的案例

    import sys
    from PyQt5.Qt import *
    

    class Window(QWidget):
    def init(self, *args, **kwargs):
    super().init(*args, **kwargs)
    self.setWindowTitle(‘动画组’)
    self.resize(500, 500)
    self.move(400, 200)
    self.btn1 = QPushButton(self)
    self.btn2 = QPushButton(self)
    self.init_ui()

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">init_ui</span><span class="hljs-params">(self)</span>:</span>
        self.btn1.resize(<span class="hljs-number">50</span>, <span class="hljs-number">50</span>)
        self.btn1.move(<span class="hljs-number">0</span>, <span class="hljs-number">0</span>)
        self.btn1.setStyleSheet(<span class="hljs-string">'QPushButton{border: none; background: pink;}'</span>)
    
        self.btn2.resize(<span class="hljs-number">50</span>, <span class="hljs-number">50</span>)
        self.btn2.move(<span class="hljs-number">50</span>, <span class="hljs-number">50</span>)
        self.btn2.setStyleSheet(<span class="hljs-string">'border: none; background: cyan'</span>)
    
        <span class="hljs-comment"># 按钮1的动画</span>
        animation1 = QPropertyAnimation(self.btn1, <span class="hljs-string">b'pos'</span>, self)
        animation1.setKeyValueAt(<span class="hljs-number">0</span>, QPoint(<span class="hljs-number">0</span>, <span class="hljs-number">0</span>))
        animation1.setKeyValueAt(<span class="hljs-number">0.25</span>, QPoint(<span class="hljs-number">450</span>, <span class="hljs-number">0</span>))
        animation1.setKeyValueAt(<span class="hljs-number">0.5</span>, QPoint(<span class="hljs-number">450</span>, <span class="hljs-number">450</span>))
        animation1.setKeyValueAt(<span class="hljs-number">0.75</span>, QPoint(<span class="hljs-number">0</span>, <span class="hljs-number">450</span>))
        animation1.setKeyValueAt(<span class="hljs-number">1</span>, QPoint(<span class="hljs-number">0</span>, <span class="hljs-number">0</span>))
        animation1.setDuration(<span class="hljs-number">5000</span>)
        <span class="hljs-comment"># animation1.start()</span>
    
        <span class="hljs-comment"># 按钮2的动画</span>
        animation2 = QPropertyAnimation(self.btn2, <span class="hljs-string">b'pos'</span>, self)
        animation2.setKeyValueAt(<span class="hljs-number">0</span>, QPoint(<span class="hljs-number">50</span>, <span class="hljs-number">50</span>))
        animation2.setKeyValueAt(<span class="hljs-number">0.25</span>, QPoint(<span class="hljs-number">400</span>, <span class="hljs-number">50</span>))
        animation2.setKeyValueAt(<span class="hljs-number">0.5</span>, QPoint(<span class="hljs-number">400</span>, <span class="hljs-number">400</span>))
        animation2.setKeyValueAt(<span class="hljs-number">0.75</span>, QPoint(<span class="hljs-number">50</span>, <span class="hljs-number">400</span>))
        animation2.setKeyValueAt(<span class="hljs-number">1</span>, QPoint(<span class="hljs-number">50</span>, <span class="hljs-number">50</span>))
        animation2.setDuration(<span class="hljs-number">3000</span>)
        <span class="hljs-comment"># animation2.start()</span>
    
        animation_group = QSequentialAnimationGroup(self)
        animation_group.addAnimation(animation1)
        animation_group.addAnimation(animation2)
        animation_group.start()
    

    if name == main:
    app = QApplication(sys.argv)
    window = Window()
    window.show()
    sys.exit(app.exec_())

    复制代码

五、关于QAbstractAnimation中事件的操作

  • 1、启动动画start()

  • 2、暂停动画pause()

  • 3、继续启动动画resume()

  • 4、停止动画stop()

  • 5、基本案例

    import sys
    from PyQt5.Qt import *
    

    class Window(QWidget):
    def init(self, *args, **kwargs):
    super().init(*args, **kwargs)
    self.setWindowTitle(‘动画组’)
    self.resize(500, 500)
    self.move(400, 200)
    self.btn1 = QPushButton(self)
    self.btn2 = QPushButton(self)
    self.init_ui()

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">init_ui</span><span class="hljs-params">(self)</span>:</span>
        self.btn1.resize(<span class="hljs-number">50</span>, <span class="hljs-number">50</span>)
        self.btn1.move(<span class="hljs-number">0</span>, <span class="hljs-number">0</span>)
        self.btn1.setStyleSheet(<span class="hljs-string">'QPushButton{border: none; background: pink;}'</span>)
    
        self.btn2.resize(<span class="hljs-number">50</span>, <span class="hljs-number">50</span>)
        self.btn2.move(<span class="hljs-number">50</span>, <span class="hljs-number">50</span>)
        self.btn2.setStyleSheet(<span class="hljs-string">'border: none; background: cyan'</span>)
    
        <span class="hljs-comment"># 按钮1的动画</span>
        animation1 = QPropertyAnimation(self.btn1, <span class="hljs-string">b'pos'</span>, self)
        animation1.setKeyValueAt(<span class="hljs-number">0</span>, QPoint(<span class="hljs-number">0</span>, <span class="hljs-number">0</span>))
        animation1.setKeyValueAt(<span class="hljs-number">0.25</span>, QPoint(<span class="hljs-number">450</span>, <span class="hljs-number">0</span>))
        animation1.setKeyValueAt(<span class="hljs-number">0.5</span>, QPoint(<span class="hljs-number">450</span>, <span class="hljs-number">450</span>))
        animation1.setKeyValueAt(<span class="hljs-number">0.75</span>, QPoint(<span class="hljs-number">0</span>, <span class="hljs-number">450</span>))
        animation1.setKeyValueAt(<span class="hljs-number">1</span>, QPoint(<span class="hljs-number">0</span>, <span class="hljs-number">0</span>))
        animation1.setDuration(<span class="hljs-number">5000</span>)
        <span class="hljs-comment"># animation1.start()</span>
    
        <span class="hljs-comment"># 按钮2的动画</span>
        animation2 = QPropertyAnimation(self.btn2, <span class="hljs-string">b'pos'</span>, self)
        animation2.setKeyValueAt(<span class="hljs-number">0</span>, QPoint(<span class="hljs-number">50</span>, <span class="hljs-number">50</span>))
        animation2.setKeyValueAt(<span class="hljs-number">0.25</span>, QPoint(<span class="hljs-number">400</span>, <span class="hljs-number">50</span>))
        animation2.setKeyValueAt(<span class="hljs-number">0.5</span>, QPoint(<span class="hljs-number">400</span>, <span class="hljs-number">400</span>))
        animation2.setKeyValueAt(<span class="hljs-number">0.75</span>, QPoint(<span class="hljs-number">50</span>, <span class="hljs-number">400</span>))
        animation2.setKeyValueAt(<span class="hljs-number">1</span>, QPoint(<span class="hljs-number">50</span>, <span class="hljs-number">50</span>))
        animation2.setDuration(<span class="hljs-number">8000</span>)
        <span class="hljs-comment"># animation2.start()</span>
    
        animation_group = QParallelAnimationGroup(self)
        animation_group.addAnimation(animation1)
        animation_group.addAnimation(animation2)
        animation_group.start()
    
        self.btn1.clicked.connect(animation_group.pause)
        self.btn2.clicked.connect(animation_group.resume)
    

    if name == main:
    app = QApplication(sys.argv)
    window = Window()
    window.show()
    sys.exit(app.exec_())

    复制代码

  • python

    Python (发音:[ paiθ(ə)n; (US) paiθɔn ]n. 蟒蛇,巨蛇 ),是一种面向对象的解释性的计算机程序设计语言,也是一种功能强大而完善的通用型语言,已经具有十多年的发…

    7952 引用 • 22 回帖 • 2 关注
感谢    赞同    分享    收藏    关注    反对    举报    ...