如何动画按钮从各个方向延伸?
How to animate the button extend form all directions?
当我在pyqt5中为按钮制作动画时。当我单击按钮时,按钮仅从两个方向延伸,如第一张图片,我希望按钮从所有四个方向延伸,如第二张图片
第一个:
第二个:
import sys
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
class main(QMainWindow):
def __init__(self):
super().__init__()
self.setGeometry(100, 100, 500, 500)
self.b = QPushButton('click', self)
self.b.resize(50, 50)
self.b.move(150, 150)
self.b.clicked.connect(self.one)
def one(self):
self.an1 = QPropertyAnimation(self.b, b'size')
self.an1.setStartValue(self.b.size())
self.an1.setEndValue(QSize(100, 100))
self.an1.setDuration(200)
self.an1.start()
if __name__ == "__main__":
app = QApplication(sys.argv)
mai = main()
mai.show()
app.exec()
您只更改大小而不更改位置,因此您有以下选项:
- 使用valueChanged信号获取当前大小并重新计算位置。
- 再创建一个根据大小改变位置的动画,与另一个动画同步。
- 在几何体上设置动画而不是尺寸。
在这种情况下,我将显示第三个选项:
def one(self):
rect_start = QRect(self.b.geometry())
rect_end = QRect(0, 0, 100, 100)
rect_end.moveCenter(rect_start.center())
an1 = QPropertyAnimation(self.b, b"geometry", self)
an1.setStartValue(rect_start)
an1.setEndValue(rect_end)
an1.setDuration(200)
an1.start(QAbstractAnimation.DeleteWhenStopped)
当我在pyqt5中为按钮制作动画时。当我单击按钮时,按钮仅从两个方向延伸,如第一张图片,我希望按钮从所有四个方向延伸,如第二张图片
第一个:
第二个:
import sys
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
class main(QMainWindow):
def __init__(self):
super().__init__()
self.setGeometry(100, 100, 500, 500)
self.b = QPushButton('click', self)
self.b.resize(50, 50)
self.b.move(150, 150)
self.b.clicked.connect(self.one)
def one(self):
self.an1 = QPropertyAnimation(self.b, b'size')
self.an1.setStartValue(self.b.size())
self.an1.setEndValue(QSize(100, 100))
self.an1.setDuration(200)
self.an1.start()
if __name__ == "__main__":
app = QApplication(sys.argv)
mai = main()
mai.show()
app.exec()
您只更改大小而不更改位置,因此您有以下选项:
- 使用valueChanged信号获取当前大小并重新计算位置。
- 再创建一个根据大小改变位置的动画,与另一个动画同步。
- 在几何体上设置动画而不是尺寸。
在这种情况下,我将显示第三个选项:
def one(self):
rect_start = QRect(self.b.geometry())
rect_end = QRect(0, 0, 100, 100)
rect_end.moveCenter(rect_start.center())
an1 = QPropertyAnimation(self.b, b"geometry", self)
an1.setStartValue(rect_start)
an1.setEndValue(rect_end)
an1.setDuration(200)
an1.start(QAbstractAnimation.DeleteWhenStopped)