pyqt中如何实现滑动效果,并举例
时间: 2023-05-23 18:03:45 浏览: 90
PyQt中实现滑动效果可以使用QPropertyAnimation类,通过设置动画对象的起始值和结束值,以及动画持续的时间等参数来实现。
以下是一个简单的滑动效果的例子:
```python
from PyQt5.QtCore import QPropertyAnimation, QPoint
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton
class Example(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setGeometry(300, 300, 300, 200)
self.setWindowTitle('Example')
# 创建一个按钮
self.button = QPushButton('Slide', self)
self.button.setGeometry(50, 50, 100, 50)
# 创建一个动画对象
self.animation = QPropertyAnimation(self.button, b"pos")
# 设置动画的起始值和结束值
start_pos = QPoint(50, 50)
end_pos = QPoint(150, 50)
self.animation.setStartValue(start_pos)
self.animation.setEndValue(end_pos)
# 设置动画的持续时间和缓动函数
self.animation.setDuration(1000)
self.animation.setEasingCurve(QEasingCurve.OutBounce)
self.show()
def mousePressEvent(self, event):
# 当鼠标单击窗口时,启动动画
self.animation.start()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
```
在这个例子中,创建了一个按钮,并使用QPropertyAnimation类创建了一个动画对象。在鼠标单击窗口时,启动动画,按钮就会从初始位置(50, 50)滑动到结束位置(150, 50)。动画的持续时间为1秒,使用OutBounce函数来实现缓动效果。