PyQt5基础学习-动态显示窗口的缩放 1.QPropertyAnimation(设置对应的属性) 2.QAnimation.setDuration(设置时间间隔) 3.QAnimation.setStartValue(设置初始值) 4.QAnimation.setEndValue(设置结束值)
使用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_())
每天更新pyQt5内容