我 的 个 人 主 页:👉👉 失心疯的个人主页 👈👈
入 门 教 程 推 荐 :👉👉 Python零基础入门教程合集 👈👈
虚 拟 环 境 搭 建 :👉👉 Python项目虚拟环境(超详细讲解) 👈👈
PyQt5 系 列 教 程:👉👉 Python GUI(PyQt5)文章合集 👈👈
Oracle数据库教程:👉👉 Oracle数据库文章合集 👈👈
优 质 资 源 下 载 :👉👉 资源下载合集 👈👈
动画组
- 描述
- 可以将一组动画,同时播放或者按顺序播放
- 子类
- 并行动画组:QParallelAnimationGroup
- 串行动画组:QSequentialAnimationGroup
- 继承自:QAnimationGroup
动画组:QAnimationGroup
- 添加动画
addAnimation(QAbstractAnimation animation) # 添加动画在最末尾 insertAnimation(int index, QAbstractAnimation animation) # 在指定索引位置插入动画 # 动画组内可以嵌套添加动画组
- 移除动画
removeAnimation(QAbstractAnimation animation) # 移除指定动画
- 获取动画
animationAt(int indea) -> QAbstractAnimation # 获取指定索引动画
- 获取并移除动画
takeAnimation(int index) -> QAbstractAnimation # 获取指定索引动画并删除
- 动画个数
animationCount() -> int # 获取动画组内动画个数
- 清空动画
clear() # 清空动画组内所有动画
并行动画组:QParallelAnimationGroup
- 描述
- 添加的所有动画,都是同时执行(同时开始、同时暂停等)
- 继承自:QAnimationGroup
- 功能作用
- 构造方法
QAnimationGroup(parent) # 创建并行动画组对象的同时设置父对象
- 其他方法全部继承自父类
- 构造方法
串行动画组:QSequentialAnimationGroup
- 描述
- 添加的所有动画都是串行顺序执行(一个动画执行完再执行另外一个动画)
- 继承自:QAnimationGroup
- 功能作用
- 构造函数
QAnimationGroup(parent) # 创建串行动画组对象的同时设置父对象
- 添加暂停
addPause(int msecs) -> QPauseAnimation # 在两个动画之间添加暂停对象,需写在两句添加动画语句中间 # 这里除了用addPause()方法添加暂停 # 还可以通过QPauseAnimation创建一个暂停动画 # 然后通过addAnimation()/insertAnimation()将这个暂停动画添加到动画组中
- 插入暂停
insertPause(int index, int msecs) -> QPauseAnimation # 在指定索引位置添加暂停对象
- 获取动画
currentAnimation() -> QPauseAnimation # 获取当前运行的动画
- 构造函数
- 示例代码
- 示例1:QParallelAnimationGroup-并行动画组
from PyQt5.Qt import * import sys class Windows(QWidget): def __init__(self): super().__init__() self.setWindowTitle('QParallelAnimationGroup-功能作用') self.resize(500, 600) self.widget_list() def widget_list(self): self.add_widget() def add_widget(self): red_btn = QPushButton(self) red_btn.resize(50, 50) red_btn.setStyleSheet('background-color: red;') green_btn = QPushButton(self) green_btn.resize(50, 50) green_btn.move(100, 100) green_btn.setStyleSheet('background-color: green;') btn = QPushButton('暂停动画', self) btn.setObjectName('btn') self.btn = btn btn.resize(100, 30) btn.move(390, 550) red_animation = QPropertyAnimation(red_btn, b'pos', self) red_animation.setKeyValues( [(0, QPoint(0, 0)), (1/4, QPoint(0, 450)), (2/4, QPoint(450, 450)), (3/4, QPoint(450, 0)), (1, QPoint(0, 0))]) red_animation.setDuration(3000) # red_animation.start() green_animation = QPropertyAnimation(green_btn, b'pos', self) green_animation.setKeyValues( [(0, QPoint(100, 100)), (1 / 4, QPoint(350, 100)), (2 / 4, QPoint(350, 350)), (3 / 4, QPoint(100, 350)), (1, QPoint(100, 100))]) green_animation.setDuration(3000) # green_animation.start() # 创建并行动画组 parallel_group = QParallelAnimationGroup(self) self.parallel_group = parallel_group # 动画组添加动画 parallel_group.addAnimation(red_animation) parallel_group.addAnimation(green_animation) # 启动动画组 parallel_group.start() QMetaObject.connectSlotsByName(self) # btn.clicked.connect(parallel_group.pause) @pyqtSlot() def on_btn_clicked(self): if self.parallel_group.state() == QAbstractAnimation.Running: self.parallel_group.pause() self.btn.setText('继续动画') elif self.parallel_group.state() == QAbstractAnimation.Paused: self.parallel_group.resume() self.btn.setText('暂停动画') if __name__ == '__main__': app = QApplication(sys.argv) window = Windows() window.show() sys.exit(app.exec_())
- 示例2:QSequentialAnimationGroup-串行动画
from PyQt5.Qt import * import sys class Windows(QWidget): def __init__(self): super().__init__() self.setWindowTitle('QSequentialAnimationGroup-功能作用') self.resize(500, 600) self.widget_list() def widget_list(self): self.add_widget() def add_widget(self): red_btn = QPushButton(self) red_btn.resize(50, 50) red_btn.setStyleSheet('background-color: red;') green_btn = QPushButton(self) green_btn.resize(50, 50) green_btn.move(100, 100) green_btn.setStyleSheet('background-color: green;') btn = QPushButton('暂停动画', self) btn.setObjectName('btn') self.btn = btn btn.resize(100, 30) btn.move(390, 550) red_animation = QPropertyAnimation(red_btn, b'pos', self) red_animation.setObjectName('red_animation') red_animation.setKeyValues( [(0, QPoint(0, 0)), (1/4, QPoint(0, 450)), (2/4, QPoint(450, 450)), (3/4, QPoint(450, 0)), (1, QPoint(0, 0))]) red_animation.setDuration(3000) # red_animation.start() green_animation = QPropertyAnimation(green_btn, b'pos', self) green_animation.setObjectName('green_animation') green_animation.setKeyValues( [(0, QPoint(100, 100)), (1 / 4, QPoint(350, 100)), (2 / 4, QPoint(350, 350)), (3 / 4, QPoint(100, 350)), (1, QPoint(100, 100))]) green_animation.setDuration(3000) # green_animation.start() # 创建并行动画组 sequential_group = QSequentialAnimationGroup(self) self.sequential_group = sequential_group sequential_group.addAnimation(red_animation) # 在两个动画之间添加暂停 # sequential_group.addPause(3000) # 也可以通过QPauseAnimation创建一个暂停动画 # pauseanimation = QPauseAnimation(self) # pauseanimation.setDuration(3000) # sequential_group.addAnimation(pauseanimation) sequential_group.addAnimation(green_animation) sequential_group.start() # sequential_group.insertPause(1, 3000) QMetaObject.connectSlotsByName(self) # btn.clicked.connect(sequential_group.pause) @pyqtSlot() def on_btn_clicked(self): # 获取到当前运动的动画对象 print(self.sequential_group.currentAnimation().objectName()) if self.sequential_group.state() == QAbstractAnimation.Running: self.sequential_group.pause() self.btn.setText('继续动画') elif self.sequential_group.state() == QAbstractAnimation.Paused: self.sequential_group.resume() self.btn.setText('暂停动画') if __name__ == '__main__': app = QApplication(sys.argv) window = Windows() window.show() sys.exit(app.exec_())
- 示例3:通过并行动画丝滑设置左右屏幕大小变化
from PyQt5.Qt import * import sys class Windows(QWidget): def __init__(self): super().__init__() self.setWindowTitle('QParallelAnimationGroup-功能作用') self.resize(500, 600) self.widget_list() def widget_list(self): self.add_widget() def add_widget(self): red_btn = QWidget(self) red_btn.setGeometry(0,0,250,600) red_btn.setStyleSheet('background-color: red;') green_btn = QWidget(self) green_btn.setGeometry(250,0,250,600) green_btn.setStyleSheet('background-color: green;') red_animation = QPropertyAnimation(red_btn, b'geometry', self) red_animation.setKeyValues( [(0, QRect(0, 0, 250, 600)), (1 / 4, QRect(0, 0, 200, 600)), (2 / 4, QRect(0, 0, 150, 600)), (3 / 4, QRect(0, 0, 100, 600)), (1, QRect(0, 0, 50, 600))]) red_animation.setDuration(3000) green_animation = QPropertyAnimation(green_btn, b'geometry', self) green_animation.setKeyValues( [(0, QRect(250,0,250,600)), (1 / 4, QRect(200,0,300,600)), (2 / 4, QRect(150,0,350,600)), (3 / 4, QRect(100,0,400,600)), (1, QRect(50,0,450,600))]) green_animation.setDuration(3000) # 创建并行动画组 parallel_group = QParallelAnimationGroup(self) self.parallel_group = parallel_group # 动画组添加动画 parallel_group.addAnimation(red_animation) parallel_group.addAnimation(green_animation) # 启动动画组 parallel_group.start() QMetaObject.connectSlotsByName(self) @pyqtSlot() def on_btn_clicked(self): if self.parallel_group.state() == QAbstractAnimation.Running: self.parallel_group.pause() self.btn.setText('继续动画') elif self.parallel_group.state() == QAbstractAnimation.Paused: self.parallel_group.resume() self.btn.setText('暂停动画') if __name__ == '__main__': app = QApplication(sys.argv) window = Windows() window.show() sys.exit(app.exec_())
- 示例1:QParallelAnimationGroup-并行动画组