pyqt5 动画学习(四) 旋转动画,使用QGraphicsView让自己的控件旋转起来
时间: 2024-02-02 09:03:49 浏览: 36
在PyQt5中,可以使用QPropertyAnimation来实现旋转动画。首先,需要导入QPropertyAnimation和QEasingCurve类:
```
from PyQt5.QtCore import QPropertyAnimation, QEasingCurve
```
然后,在需要旋转的控件的类中,添加一个旋转属性:
```
class MyWidget(QWidget):
def __init__(self):
super().__init__()
self.angle = 0
```
接下来,定义一个旋转动画函数:
```
def rotateAnimation(self):
animation = QPropertyAnimation(self, b"angle")
animation.setDuration(1000) # 持续时间为1秒
animation.setStartValue(0) # 起始值为0度
animation.setEndValue(360) # 结束值为360度
animation.setEasingCurve(QEasingCurve.InOutQuad) # 缓动函数为InOutQuad
animation.start()
```
在该函数中,创建一个QPropertyAnimation对象并将其绑定到控件的angle属性上。设置旋转动画的持续时间、起始值、结束值和缓动函数。最后,调用start()函数开始旋转动画。
接下来,在需要使用旋转动画的地方调用该函数即可:
```
myWidget = MyWidget()
myWidget.rotateAnimation()
```
此时,myWidget控件将会绕着中心点旋转360度,持续时间为1秒。可以根据需求自行修改动画参数。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)