如何让一个QLabel旋转
时间: 2024-05-09 13:16:04 浏览: 134
要让一个QLabel旋转,可以使用QPropertyAnimation和QTransform。
以下是一个简单的示例代码:
``` python
from PyQt5.QtCore import Qt, QPropertyAnimation
from PyQt5.QtGui import QTransform
from PyQt5.QtWidgets import QApplication, QLabel, QWidget
class RotatingLabel(QLabel):
def __init__(self, parent=None):
super().__init__(parent)
self.angle = 0.0
def setAngle(self, angle):
self.angle = angle
transform = QTransform().rotate(self.angle)
self.setPixmap(self.originalPixmap.transformed(transform))
if __name__ == '__main__':
app = QApplication([])
widget = QWidget()
label = RotatingLabel(widget)
label.setPixmap(QPixmap('image.png'))
label.show()
animation = QPropertyAnimation(label, b'angle')
animation.setDuration(2000)
animation.setStartValue(0.0)
animation.setEndValue(360.0)
animation.setLoopCount(-1)
animation.start()
app.exec_()
```
在这个例子中,我们创建了一个名为RotatingLabel的子类QLabel。该类有一个名为angle的属性,表示旋转角度。然后我们使用QTransform旋转原始QPixmap并将其设置为标签的当前像素图像。通过使用QPropertyAnimation来动画旋转角度,我们可以让标签旋转。
阅读全文
相关推荐















