pyqt5中动画的使用详解


Posted in Python onApril 01, 2020

一、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()

  def init_ui(self):
    self.btn.resize(100, 100)
    self.btn.move(0, 0)
    self.btn.setStyleSheet('QPushButton{border: none; background: pink;}')

    # 1.定义一个动画
    animation = QPropertyAnimation(self)
    animation.setTargetObject(self.btn)
    animation.setPropertyName(b'pos')
    # 使用另外一种构造函数方式创建
    # animation = QPropertyAnimation(self.btn, b'pos', self)

    # 2.设置属性值
    animation.setStartValue(QPoint(0, 0))
    animation.setEndValue(QPoint(400, 400))

    # 3.设置时长
    animation.setDuration(3000)

    # 4.启动动画
    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()

  def init_ui(self):
    self.btn.resize(50, 50)
    self.btn.move(0, 0)
    self.btn.setStyleSheet('QPushButton{border: none; background: pink;}')
    
    # 1.创建动画
    animation = QPropertyAnimation(self.btn, b'pos', self)
    
    # 2.定义动画插值
    animation.setKeyValueAt(0, QPoint(0, 0))
    animation.setKeyValueAt(0.25, QPoint(450, 0))
    animation.setKeyValueAt(0.5, QPoint(450, 450))
    animation.setKeyValueAt(0.75, QPoint(0, 450))
    animation.setKeyValueAt(1, QPoint(0, 0))
    # 3.动画时长
    animation.setDuration(5000)
    # 4.启动动画
    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()

  def init_ui(self):
    self.btn1.resize(50, 50)
    self.btn1.move(0, 0)
    self.btn1.setStyleSheet('QPushButton{border: none; background: pink;}')

    self.btn2.resize(50, 50)
    self.btn2.move(50, 50)
    self.btn2.setStyleSheet('border: none; background: cyan')

    # 按钮1的动画
    animation1 = QPropertyAnimation(self.btn1, b'pos', self)
    animation1.setKeyValueAt(0, QPoint(0, 0))
    animation1.setKeyValueAt(0.25, QPoint(450, 0))
    animation1.setKeyValueAt(0.5, QPoint(450, 450))
    animation1.setKeyValueAt(0.75, QPoint(0, 450))
    animation1.setKeyValueAt(1, QPoint(0, 0))
    animation1.setDuration(5000)
    # animation1.start()

    # 按钮2的动画
    animation2 = QPropertyAnimation(self.btn2, b'pos', self)
    animation2.setKeyValueAt(0, QPoint(50, 50))
    animation2.setKeyValueAt(0.25, QPoint(400, 50))
    animation2.setKeyValueAt(0.5, QPoint(400, 400))
    animation2.setKeyValueAt(0.75, QPoint(50, 400))
    animation2.setKeyValueAt(1, QPoint(50, 50))
    animation2.setDuration(3000)
    # animation2.start()

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

  def init_ui(self):
    self.btn1.resize(50, 50)
    self.btn1.move(0, 0)
    self.btn1.setStyleSheet('QPushButton{border: none; background: pink;}')

    self.btn2.resize(50, 50)
    self.btn2.move(50, 50)
    self.btn2.setStyleSheet('border: none; background: cyan')

    # 按钮1的动画
    animation1 = QPropertyAnimation(self.btn1, b'pos', self)
    animation1.setKeyValueAt(0, QPoint(0, 0))
    animation1.setKeyValueAt(0.25, QPoint(450, 0))
    animation1.setKeyValueAt(0.5, QPoint(450, 450))
    animation1.setKeyValueAt(0.75, QPoint(0, 450))
    animation1.setKeyValueAt(1, QPoint(0, 0))
    animation1.setDuration(5000)
    # animation1.start()

    # 按钮2的动画
    animation2 = QPropertyAnimation(self.btn2, b'pos', self)
    animation2.setKeyValueAt(0, QPoint(50, 50))
    animation2.setKeyValueAt(0.25, QPoint(400, 50))
    animation2.setKeyValueAt(0.5, QPoint(400, 400))
    animation2.setKeyValueAt(0.75, QPoint(50, 400))
    animation2.setKeyValueAt(1, QPoint(50, 50))
    animation2.setDuration(8000)
    # animation2.start()

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

到此这篇关于pyqt5中动画的使用详解的文章就介绍到这了,更多相关pyqt5 动画内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Python 相关文章推荐
下载糗事百科的内容_python版
Dec 07 Python
python 排列组合之itertools
Mar 20 Python
python实现监控linux性能及进程消耗性能的方法
Jul 25 Python
python numpy函数中的linspace创建等差数列详解
Oct 13 Python
python线程池threadpool使用篇
Apr 27 Python
用Python实现大文本文件切割的方法
Jan 12 Python
python学习开发mock接口
Apr 28 Python
python实现两个经纬度点之间的距离和方位角的方法
Jul 05 Python
Python生态圈图像格式转换问题(推荐)
Dec 02 Python
python uuid生成唯一id或str的最简单案例
Jan 13 Python
python3读取文件指定行的三种方法
May 24 Python
Python实现科学占卜 让视频自动打码
Apr 09 Python
django项目中新增app的2种实现方法
Apr 01 #Python
Django Admin设置应用程序及模型顺序方法详解
Apr 01 #Python
django API 中接口的互相调用实例
Apr 01 #Python
完美解决pyinstaller打包报错找不到依赖pypiwin32或pywin32-ctypes的错误
Apr 01 #Python
Python greenlet和gevent使用代码示例解析
Apr 01 #Python
Django-rest-framework中过滤器的定制实例
Apr 01 #Python
Python如何操作office实现自动化及win32com.client的运用
Apr 01 #Python
You might like
怎么样可以把 phpinfo()屏蔽掉?
2006/11/24 PHP
基于PHP5魔术常量与魔术方法的详解
2013/06/13 PHP
php strrpos()与strripos()函数
2013/08/31 PHP
PHP中source #N问题的解决方法
2014/01/27 PHP
Centos PHP 扩展Xchche的安装教程
2016/07/09 PHP
Laravel模型事件的实现原理详解
2018/03/14 PHP
Laravel Eloquent ORM 多条件查询的例子
2019/10/10 PHP
PHP与SQL语句写一句话木马总结
2019/10/11 PHP
基于jQuery的可以控制左右滚动及自动滚动效果的代码
2010/07/25 Javascript
不使用中间变量,交换int型的 a, b两个变量的值。
2010/10/29 Javascript
微信小程序 页面之间传参实例详解
2017/01/13 Javascript
jQuery读取本地的json文件(实例讲解)
2017/10/31 jQuery
javaScript字符串工具类StringUtils详解
2017/12/08 Javascript
js时间戳与日期格式之间转换详解
2017/12/11 Javascript
Angular5升级RxJS到5.5.3报错:EmptyError: no elements in sequence的解决方法
2018/04/09 Javascript
详解小程序rich-text对富文本支持方案
2018/11/28 Javascript
Vuex的actions属性的具体使用
2019/04/14 Javascript
Vue路由之JWT身份认证的实现方法
2019/08/26 Javascript
详解Howler.js Web音频播放终极解决方案
2020/08/23 Javascript
JavaScript实现下拉列表
2021/01/20 Javascript
[03:59]5分钟带你了解什么是DOTA2(第二期)
2017/02/07 DOTA
Python查询Mysql时返回字典结构的代码
2012/06/18 Python
python 把文件中的每一行以数组的元素放入数组中的方法
2018/04/29 Python
读取json格式为DataFrame(可转为.csv)的实例讲解
2018/06/05 Python
对django2.0 关联表的必填on_delete参数的含义解析
2019/08/09 Python
基于python+selenium的二次封装的实现
2020/01/06 Python
Python面向对象多态实现原理及代码实例
2020/09/16 Python
Python安装Bs4的多种方法
2020/11/28 Python
基于Modernizr 让网站进行优雅降级的分析
2013/04/21 HTML / CSS
如何利用find命令查找文件
2016/11/18 面试题
安全生产责任书
2014/03/12 职场文书
出国留学导师推荐信
2015/03/26 职场文书
城南旧事观后感
2015/06/11 职场文书
2015年国庆节标语大全
2015/07/30 职场文书
入党宣誓大会后的感想
2015/08/10 职场文书
Nginx服务器添加Systemd自定义服务过程解析
2021/03/31 Servers