pyqt5 实现按钮旋转动画
时间: 2023-10-12 21:05:57 浏览: 258
可以使用QPropertyAnimation类来实现按钮旋转动画。首先,需要在Qt Designer中创建一个QPushButton,并给它添加一个样式表。然后,在PyQt5代码中,创建一个QPropertyAnimation对象,将其目标对象设置为QPushButton,将属性设置为“rotation”,并指定动画的持续时间和结束值。最后,启动动画并将其应用于按钮。
下面是一个示例代码:
```
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton
from PyQt5.QtCore import QPropertyAnimation, QVariantAnimation, Qt
class Example(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setGeometry(300, 300, 300, 200)
button = QPushButton('Click', self)
button.setStyleSheet('background-color: #FF9D6E; color: white; font-size: 20px; padding: 10px 20px; border-radius: 10px;')
button.clicked.connect(self.rotateButton)
button.resize(100, 50)
button.move(100, 75)
self.show()
def rotateButton(self):
button = self.sender()
animation = QPropertyAnimation(button, b"rotation")
animation.setDuration(1000)
animation.setEasingCurve(QVariantAnimation.Linear)
animation.setStartValue(0)
animation.setEndValue(360)
animation.setLoopCount(-1)
animation.start()
if __name__ == '__main__':
app = QApplication([])
ex = Example()
app.exec_()
```
在这个示例中,我们创建了一个QPushButton并给它添加了一个样式表。当用户单击按钮时,我们将创建一个QPropertyAnimation对象并将其应用于该按钮。我们指定动画的旋转属性,持续时间,结束值和循环次数。最后,我们启动动画并将其应用于按钮。
希望这可以帮助你实现按钮旋转动画!
阅读全文
相关推荐


















