pyqt5 不使用QPropertyAnimation实现按钮旋转动画
时间: 2024-06-08 17:06:53 浏览: 178
可以使用QTransform来实现按钮的旋转动画。具体步骤如下:
1. 导入必要的模块
```python
from PyQt5.QtCore import QPropertyAnimation, QTransform, QRect
from PyQt5.QtWidgets import QPushButton
```
2. 创建QPushButton对象并设置初始状态
```python
button = QPushButton('Rotate')
button.setGeometry(QRect(50, 50, 100, 50))
button.show()
```
3. 创建QTransform对象并设置初始状态
```python
transform = QTransform()
transform.rotate(0) # 初始状态为0度旋转
```
4. 创建QPropertyAnimation对象并设置属性
```python
animation = QPropertyAnimation(button, b"transform")
animation.setDuration(1000) # 动画持续时间为1秒
animation.setStartValue(transform) # 设置起始状态
```
5. 在槽函数中更新QTransform对象并启动动画
```python
def rotate_button():
global transform
transform.rotate(90) # 每次旋转90度
animation.setEndValue(transform) # 设置终止状态
animation.start()
button.clicked.connect(rotate_button)
```
完整代码如下:
```python
from PyQt5.QtCore import QPropertyAnimation, QTransform, QRect
from PyQt5.QtWidgets import QPushButton
button = QPushButton('Rotate')
button.setGeometry(QRect(50, 50, 100, 50))
button.show()
transform = QTransform()
transform.rotate(0)
animation = QPropertyAnimation(button, b"transform")
animation.setDuration(1000)
animation.setStartValue(transform)
def rotate_button():
global transform
transform.rotate(90)
animation.setEndValue(transform)
animation.start()
button.clicked.connect(rotate_button)
```
阅读全文
相关推荐


















