代码:
1 # -*- coding:utf8 -*- 2 from PyQt4.QtGui import * 3 from PyQt4.QtCore import * 4 import sys 5 6 class ani(QWidget): 7 def __init__(self): 8 super(ani, self).__init__() 9 self.OrigHeight = 50 10 self.ChangeHeight = 150 11 self.setGeometry(QRect(500, 400, 150, self.OrigHeight)) # 在X=500, Y=400 , Length=150 , Height=50 12 self.btn = QPushButton(u'展开', self) 13 self.btn.setGeometry(10, 10, 60, 35) 14 self.machine = QStateMachine() 15 self.connect(self.btn, SIGNAL('clicked()'), self.change) 16 17 # 动画效果修改窗体大小 18 def change(self): 19 CurrentHeight = self.height() 20 if self.OrigHeight == CurrentHeight: 21 startHeight = self.OrigHeight 22 endHeight = self.ChangeHeight 23 self.btn.setText(u'收缩') 24 else: 25 startHeight = self.ChangeHeight 26 endHeight = self.OrigHeight 27 self.btn.setText(u'展开') 28 self.animation = QPropertyAnimation(window, 'geometry') 29 self.animation.setDuration(800) 30 self.animation.setStartValue(QRect(500, 400, 150, startHeight)) 31 self.animation.setEndValue(QRect(500, 400, 150, endHeight)) 32 self.animation.start() 33 34 35 36 if __name__ == '__main__': 37 app = QApplication(sys.argv) 38 window = ani() 39 window.show() 40 sys.exit(app.exec_())
效果: