学不完,根本学不完:(,感觉逐渐陷入了学习深渊。。。
QPropertyAnimation 是 PySide(PyQt) 中一个用于在时间轴上平滑地改变对象属性的类。它常用于制作动画效果,比如移动、缩放或改变透明度等。
基本概念
         QPropertyAnimation 是 Qt 动画框架的一部分,它能够让你在一定的时间内渐变地改变一个对象的属性。比如,你可以用它来改变一个窗口的位置、大小,或者改变一个按钮的颜色。
主要功能
         属性动画:QPropertyAnimation 允许你定义要动画化的属性,并设置它的起始值和结束值。
         时间轴:你可以设置动画的持续时间,并决定动画的开始、结束以及加速、减速方式(比如线性、加速、减速)。
         缓动曲线:可以使用不同的缓动曲线(easing curve)来控制动画的变化方式。
         目标对象:动画可以作用于任何支持属性的 Qt 对象。
常用方法
         构造函数:
QPropertyAnimation(target, property_name)target 是要应用动画的对象。
 property_name 是要动画化的属性名称(例如 "pos" 或 "size")。
设置动画范围:
animation.setStartValue(start_value)
animation.setEndValue(end_value)start_value 是动画开始时属性的值。
 end_value 是动画结束时属性的值。
设置动画持续时间:
animation.setDuration(duration)duration 是动画的持续时间,单位是毫秒。
设置缓动曲线:
animation.setEasingCurve(easing_curve)easing_curve 是缓动曲线的类型,例如 QEasingCurve.Linear, QEasingCurve.InOutQuad 等。
启动动画:
animation.start()动画更新:
 QPropertyAnimation 还支持更新动画的值,比如你可以设置动画的播放进度或者暂停、停止动画。例如:
设置动画的播放进度:
animation.setCurrentTime(1000)  # 设置动画到 1000 毫秒的位置暂停播放
animation.pause()恢复播放
animation.resume()停止播放
animation.stop()  # 停止动画示例代码,展示了如何使用 QPropertyAnimation 来动画化一个小部件的宽度:
from PyQt5.QtCore import QPropertyAnimation, QSize, QEasingCurve
from PyQt5.QtWidgets import QApplication, QVBoxLayout, QWidget, QLabel
app = QApplication([])
# 创建一个窗口
window = QWidget()
window.setGeometry(100, 100, 800, 300)
layout = QVBoxLayout(window)
# 创建一个标签
label = QLabel("Animate Me")
label.setStyleSheet('border:2px solid #b6b6b6;background-color:#e0e0e0;')
label.setFixedHeight(40)  # 固定按钮的高度
layout.addWidget(label)
# 创建 QPropertyAnimation 对象
animation = QPropertyAnimation(label, b"size")
# 设置动画的起始值和结束值
start_size = label.size()
end_size = QSize(300, label.height())  # 目标宽度为300,高度保持不变
animation.setStartValue(start_size)
animation.setEndValue(end_size)
# 设置动画持续时间(2000 毫秒即 2 秒)
animation.setDuration(2000)
# 设置缓动曲线为线性
animation.setEasingCurve(QEasingCurve.Linear)
# 启动动画
animation.start()
window.show()
app.exec_()一些其他的设置:
设置动画的目标对象和属性:
animation.setTargetObject(target)
animation.setPropertyName(property_name)
动画的进度和状态:
animation.currentTime(): 获取动画当前时间。
animation.currentValue(): 获取动画当前属性值。
animation.state(): 获取动画状态,如 QAbstractAnimation.Running 或 QAbstractAnimation.Stopped。注意事项
-  属性名称:确保你动画化的属性是目标对象支持的属性。如果属性名称不正确,动画不会按预期工作。 
-  线程安全: QPropertyAnimation通常在主线程中运行,确保不要在多个线程中同时操作同一个动画对象。
-  动画回调:你可以连接动画的信号(如 finished()信号)来执行动画完成后的操作:animation.finished.connect(lambda: print("Animation finished!"))



















