使用QPropertyAnimation動態的設置屬性, 從而實現動畫的效果
AnimWindow.py
""" 用動畫效果改變窗口尺寸 QPropertyAnimation """ from PyQt5.QtGui import * from PyQt5.QtCore import * from PyQt5.QtWidgets import * import sys class AnimWindow(QWidget): def __init__(self): super(AnimWindow, self).__init__() self.OrigHeight = 50 self.ChangeHeight = 150 self.setGeometry(QRect(500, 400, 150, self.OrigHeight)) self.btn = QPushButton('展開', self) self.btn.setGeometry(10, 10, 60, 35) self.btn.clicked.connect(self.change) def change(self): currentHeight = self.height() #根據當前的高度,來判斷當前是收縮狀態還是展開狀態 if self.OrigHeight == currentHeight: startHeight = self.OrigHeight endHeight = self.ChangeHeight self.btn.setText('收縮') else: startHeight = self.ChangeHeight endHeight = self.OrigHeight self.btn.setText("展開") #動態設置屬性 self.animation = QPropertyAnimation(self, b'geometry') self.animation.setDuration(500) self.animation.setStartValue(QRect(500, 400, 150, startHeight)) self.animation.setEndValue(QRect(500, 400, 150, endHeight)) self.animation.start() if __name__ == "__main__": app = QApplication(sys.argv) win = AnimWindow() win.show() sys.exit(app.exec_())