PyQt5 用动画效果改变窗口的尺寸 案例
QPropertyAnimation
import sys
from PyQt5.QtCore import Qt, QRect, QPropertyAnimation
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QHBoxLayout, QPushButton, QMessageBox, QApplication, QVBoxLayout, QWidget, \
QLabel, QGridLayout, QLineEdit, QTextEdit, QFormLayout, QComboBox, QMainWindow
from CommonHelper import CommonHelper
'''
PyQt5 用动画效果改变窗口的尺寸 案例
QPropertyAnimation
'''
class AnimWindowDemo(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.OrigHeight = 50;
self.ChangeHeight = 150;
# 设置定位和左上角坐标
self.setGeometry(QRect(500,400,150,self.OrigHeight))
# 设置窗口标题
self.setWindowTitle('动画效果改变窗口大小尺寸 的演示')
# 设置窗口图标
# self.setWindowIcon(QIcon('../web.ico'))
# self.resize(477,258)
self.btn = QPushButton('展开',self)
self.btn.setToolTip('提示文本')
self.btn.setGeometry(10,10,60,35)
self.btn.clicked.connect(self.change)
vbox = QVBoxLayout()
vbox.addWidget(self.btn)
self.setLayout(vbox)
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()
def mousePressEvent(self,event):
# 左击
if event.button() == Qt.LeftButton:
print(event.globalPos())
print(event.pos())
print(self.pos())
# 右击
if event.button() == Qt.RightButton:
self.close()
if __name__ == '__main__':
app = QApplication(sys.argv)
# 设置应用图标
app.setWindowIcon(QIcon('../web.ico'))
w = AnimWindowDemo()
# 0-1,1表示不透明,0表示完全透明
# w.setWindowOpacity(0.7)
w.show()
sys.exit(app.exec_())