pyqt5设置点击按钮让按钮背景图抖动
时间: 2023-07-27 22:17:47 浏览: 144
你可以使用QPropertyAnimation类来实现这个效果。首先,将按钮设置为QAbstractButton类型,然后使用setStyleSheet()方法将按钮的背景设置为一张图片。接下来,创建一个QPropertyAnimation对象,将其目标对象设置为按钮的geometry属性,将动画的持续时间设置为200毫秒。然后,设置动画的缓和曲线为QEasingCurve类型的OutBounce,这将使得动画的结束效果看起来更加自然。最后,将动画的属性设置为按钮的geometry属性,在动画结束时,将按钮的geometry属性设置为原始的位置,以实现抖动的效果。下面是一个示例代码:
```python
from PyQt5.QtCore import QPropertyAnimation, QRect
from PyQt5.QtGui import QPixmap
from PyQt5.QtWidgets import QApplication, QPushButton
app = QApplication([])
button = QPushButton()
button.setFixedSize(100, 100)
button.setStyleSheet("QPushButton{border-image:url(background.png)}")
animation = QPropertyAnimation(button, b"geometry", duration=200)
animation.setEasingCurve(QEasingCurve.OutBounce)
animation.setKeyValueAt(0, QRect(0, 0, 100, 100))
animation.setKeyValueAt(0.5, QRect(-10, 0, 120, 100))
animation.setKeyValueAt(1, QRect(0, 0, 100, 100))
animation.setLoopCount(3)
def start_animation():
animation.start()
button.clicked.connect(start_animation)
button.show()
app.exec_()
```
在这个示例中,当按钮被点击时,它的背景图将以一个抖动的效果进行动画。你可以根据需要调整动画的参数来获得更好的效果。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)