屬性動畫QPropertyAnimation
繼承於 QAbstractAnimation-->QVariantAnimation-->QPropertyAnimation
改變大小、顏色或位置是動畫中的常見操作,而QPropertyAnimation類可以修改控件的屬性值
from PyQt5.QtWidgets import QApplication, QWidget,QPushButton import sys from PyQt5.QtCore import QPropertyAnimation,QPoint,QSize,QRect,QEasingCurve class win(QWidget): def __init__(self): super().__init__() self.resize(400,400) self.setWindowTitle('動畫學習') btn=QPushButton('按鈕',self) btn.move(100,100) btn.resize(100,100) btn.setStyleSheet('background-color:yellow') #ani=QPropertyAnimation(btn,b'pos',self) #創建動畫對象 ani = QPropertyAnimation(self) #創建動畫對象 ani.setTargetObject(btn) #設置動畫目標對象 #ani.setTargetObject(self) ani.setPropertyName(b'pos') #設置動畫屬性 #注意:字節類型 #pos---位置動畫---QPoint #size---大小動畫---QSize #geometry----位置+大小動畫----QRect #windowOpacity---窗口的透明度(0.0是透明的 1.0是不透明)---好像只適合頂層窗口 ani.setStartValue(QPoint(0,0)) #設置開始位置---按鈕的左上角位置 ani.setEndValue(QPoint(300,300)) #設置結束位置 #ani.setStartValue(QSize(0, 0)) # 設置開始大小 #ani.setEndValue(QSize(300, 300)) # 設置結束大小 #ani.setStartValue(QRect(0, 0,100,100)) # 設置開始位置和大小 #ani.setEndValue(QRect(100,100,300, 300)) # 設置結束位置和大小 #ani.setStartValue(1) # 設置開始不透明 #ani.setKeyValueAt(0.5,0.2)#在動畫的某時間點插入一個值 #參數1 0.0到1.0 0.0表示開始點,1.0表示結束點 #在動畫的中間插入透明度0.2 #ani.setKeyValueAt(1, 1) #在動畫的結束點是不透明的 #ani.setEndValue(0) # 設置結束透明 ani.setDuration(5000) #設置動畫單次時長---單位毫秒 ani.setEasingCurve(QEasingCurve.InQuad) #設置動畫的節奏 #取值 https://doc.qt.io/qt-5/qeasingcurve.html#Type-enum ani.start() #動畫開始---非阻塞 if __name__=='__main__': app=QApplication(sys.argv) w=win() w.show() sys.exit(app.exec_())
屬性動畫的父類功能
from PyQt5.QtWidgets import QApplication, QWidget,QPushButton import sys from PyQt5.QtCore import QPropertyAnimation,QPoint,QSize,QRect,QEasingCurve,QAbstractAnimation class win(QWidget): def __init__(self): super().__init__() self.resize(400,400) self.setWindowTitle('動畫學習') btn=QPushButton('按鈕',self) btn.move(100,100) btn.resize(100,100) btn.setStyleSheet('background-color:yellow') btn.clicked.connect(self.AA) btn1 = QPushButton('暫停', self) btn1.clicked.connect(self.BB) btn1.move(10,150) btn2 = QPushButton('恢復暫停', self) btn2.clicked.connect(self.CC) btn2.move(10, 200) btn3 = QPushButton('停止', self) btn3.clicked.connect(self.DD) btn3.move(10, 250) btn4 = QPushButton('設置時間', self) btn4.clicked.connect(self.EE) btn4.move(10, 300) ani = QPropertyAnimation(self) self.ani=ani ani.setTargetObject(btn) ani.setPropertyName(b'pos') ani.setStartValue(QPoint(0,0)) ani.setEndValue(QPoint(300,300)) ani.setDuration(5000) ani.setEasingCurve(QEasingCurve.InQuad) ani.setLoopCount(3) #設置動畫次數---默認1次 ani.setDirection(QAbstractAnimation.Forward) #設置動畫方向 #QAbstractAnimation.Backward=1 動畫的當前時間隨着時間減少(即,從結束/持續時間向0移動)---倒序 #QAbstractAnimation.Forward=0 動畫的當前時間隨着時間而增加(即,從0移動到結束/持續時間)---順序 #信號 ani.currentLoopChanged.connect(self.FF) #循環遍數發生變化時 #會向槽函數傳遞一個參數---當前循環的遍數 #directionChanged(QAbstractAnimation.Direction newDirection) 動畫方向發生改變時 #會向槽函數傳遞一個參數---動畫新方向 ani.finished.connect(self.HH) #動畫完成時 ani.stateChanged.connect(self.GG) #狀態發生改變時 # 會向槽函數傳遞兩個參數---新狀態和老狀態 ani.start() #啟動動畫 #參數 QAbstractAnimation.KeepWhenStopped 停止時不會刪除動畫 # QAbstractAnimation.DeleteWhenStopped 停止時動畫將自動刪除 def AA(self): s=self.ani.loopCount() #返回動畫總循環次數 print('動畫總循環次數',s) s = self.ani.currentLoop() # 返回當前是循環中的第幾次---0次開始 #如果setDirection設置為倒序,返回值也是倒序 print('當前次數是:', s) s = self.ani.duration() #返回單次時長 print('單次時長是:', s) s = self.ani.currentLoopTime() # 當前單次循環內的時間 # 如果setDirection設置為倒序,返回值也是倒序 print('當前單次循環內的時間是:', s) s = self.ani.currentTime() #當前總循環中時長 #如果setDirection設置為倒序,返回值也是倒序 print('當前總循環內時長是:', s) s=self.ani.direction() #返回動畫方向 # 返回值 int #1 表示倒序;0 表示 順序 print('動畫方向:',s) def BB(self): #self.ani.pause() #暫停 self.ani.setPaused(True) #是否暫停 s=self.ani.State() #返回動畫的狀態 #QAbstractAnimation.Paused=1 暫停狀態 #QAbstractAnimation.Stopped=0 停止狀態 #QAbstractAnimation.Running=2 運行狀態 print(s) def CC(self): #self.ani.resume() #恢復暫停--繼續 self.ani.setPaused(False) # 是否暫停 s = self.ani.State() # 返回動畫的狀態 print(s) def DD(self): self.ani.stop() #停止 def EE(self): self.ani.setCurrentTime(14000) #設置當前動畫時間--按總時長計算--毫秒 def FF(self,n): print('當前循環遍數是:',n) def HH(self): print('動畫播放完畢') def GG(self,z,z1): print('新狀態是:',z) print('老狀態是:',z1) if __name__=='__main__': app=QApplication(sys.argv) w=win() w.show() sys.exit(app.exec_())
天子驕龍