Python桌面开发pyqt5—动画

本文详细介绍了Qt框架中的QPropertyAnimation,包括其在创建动画、控制属性变化、设置动画曲线、透明度动画、时间倒流、状态管理和信号连接等方面的应用,以及动画组的并发和顺序执行方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

流程

btn = QPushButton('按钮', self)
btn.resize(100, 100)
btn.move(10, 10)

btn_1 = QPushButton('按钮_1', self)
btn_1.resize(10, 10)
btn_1.move(200, 150)

# 0.创建一个动画对象,并设置目标、属性
animation = QPropertyAnimation(btn, b'pos', self)
animation_1 = QPropertyAnimation(self)
animation_1.setTargetObject(btn_1)
animation_1.setPropertyName(b'size')

# 1.设置属性值,如开始值、插值、结束值
animation.setStartValue(QPoint(0, 0))
animation.setEndValue(QPoint(400, 200))
animation_1.setStartValue(QSize(0, 0))
animation_1.setEndValue(QSize(200, 200))

# 2.动画时常
animation.setDuration(3000)
animation_1.setDuration(3000)

# 3.启动动画
animation.start()
animation_1.start()

在这里插入图片描述

获取信息

animation.loopCount()		# 一共有几次循环
animation.currentLoop()		# 当前循环的索引
animation.duration()		# 单次循环的时间
animation.totalDuration()	# 一共运行的时间
animation.currentLoopTime()	# 单次循环到了几秒
animation.currentTime()		# 总时间走到了第几秒

动画曲线

btn = QPushButton('按钮', self)
btn.resize(100, 100)
btn.move(10, 10)

animation = QPropertyAnimation(btn, b'geometry', self)
animation.setStartValue(QRect(0, 0, 0, 0))
animation.setEndValue(QRect(400, 200, 100, 100))
animation.setDuration(3000)
animation.setEasingCurve(QEasingCurve.OutQuad)  # 设置动画曲线
animation.start()
"""InBounce		#弹簧进入
OutBounce		#弹簧弹出
InQuard			#越来越慢
OutQuard			#越来越快"""

插值和透明度动画

btn = QPushButton('按钮', self)
btn.resize(100, 100)
btn.move(10, 10)

animation = QPropertyAnimation(self, b'windowOpacity', self)
animation.setStartValue(1)
animation.setKeyValueAt(0.5, 0)  # 设置插值
animation.setEndValue(1)
animation.setDuration(3000)
animation.setLoopCount(3)  # 设置循环次数
animation.start()

时间倒流

animation = QPropertyAnimation(btn, b'geometry', self)
animation.setStartValue(QRect(0, 0, 10, 10))
animation.setEndValue(QRect(400, 200, 100, 100))
animation.setDuration(3000)

# 倒流
animation.setDirection(QAbstractAnimation.Backward)
animation.start()

状态设置

btn = QPushButton('按钮', self)
def func():
    if animation.state() == QAbstractAnimation.Running:
        animation.pause()				# 暂停动画	
    elif animation.state() == QAbstractAnimation.Paused:
        animation.resume()				# 继续(恢复)动画
    elif animation.state() == QAbstractAnimation.Stopped:
        animation.stop()				# 停止动画(无法恢复)
        print('经stop的无法恢复')
btn.clicked.connect(func)

信号

animation = QPropertyAnimation(btn, b'pos', self)
animation.setStartValue(QPoint(0, 0))
animation.setEndValue(QPoint(450, 250))
animation.setDuration(3000)
animation.setLoopCount(3)
animation.start()

# 循环次数改变的信号
animation.currentLoopChanged.connect(lambda value: print('循环次数改变了:', value))
# 动画全部执行完成的信号
animation.finished.connect(lambda: print('动画全部执行完成'))
# 状态改变的信号
animation.stateChanged.connect(lambda aft, bef: print('从状态{}变为了状态{}'.format(bef, aft)))
# 方向改变的信号
animation.directionChanged.connect(lambda aft, bef: print('从方向{}变为了方向{}'.format(bef, aft)))

动画组

btn_red = QPushButton('red', self)
btn_green = QPushButton('green', self)
btn_red.setStyleSheet('background-color:red;')
btn_green.setStyleSheet('background-color:green;')
btn_red.resize(50, 50)
btn_green.resize(50, 50)
btn_red.move(0, 0)
btn_green.move(60, 60)

# 设置动画组
animation = QPropertyAnimation(btn_red, b'pos', self)
animation_1 = QPropertyAnimation(btn_green, b'pos', self)

animation.setKeyValueAt(0.0, QPoint(0, 0))
animation.setKeyValueAt(0.25, QPoint(0, 450))
animation.setKeyValueAt(0.5, QPoint(450, 450))
animation.setKeyValueAt(0.75, QPoint(450, 0))
animation.setKeyValueAt(1.0, QPoint(0, 0))
animation_1.setKeyValueAt(0.0, QPoint(60, 60))
animation_1.setKeyValueAt(0.25, QPoint(390, 60))
animation_1.setKeyValueAt(0.5, QPoint(390, 390))
animation_1.setKeyValueAt(0.75, QPoint(60, 390))
animation_1.setKeyValueAt(1.0, QPoint(60, 60))

animation.setLoopCount(3)
animation_1.setLoopCount(3)

animation.setDuration(10000)
animation_1.setDuration(10000)

# 非阻塞(并发式)运行
animation_group = QParallelAnimationGroup(self)
animation_group.addAnimation(animation)
animation_group.addAnimation(animation_1)
animation_group.start()
btn_red.clicked.connect(animation_group.pause)
btn_green.clicked.connect(animation_group.resume)

# 堵塞式运行
animation_group_1 = QSequentialAnimationGroup(self)
animation_group_1.addAnimation(animation)
animation_group_1.addPause(2000)  # 执行完第一个后暂停2秒再执行第二个
animation_group_1.addAnimation(animation_1)
animation_group_1.start()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值