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_())

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM