pyqt5中動畫的使用

二、關於 QAbstractAnimation
父類的認識
-
1、主要作用
- 繼承此類, 實現一些自定義動畫
- 所有動畫共享的功能
-
2、功能作用
- 迴圈操作
setLoopCount(count) currentLoop() currentLoopTime()
- 時間操作
duration() totalDuration() currentTime()
- 動畫方向
-
setDirection(QAbstractAnimation.Forward/QAbstractAnimation.Backward)
-
- 動畫狀態
state()
QAbstractAnimation.Stopped QAbstractAnimation.Paused QAbstractAnimation.Running
- 迴圈操作
三、 QPropertyAnimation
屬性動畫的使用
主要用於實現某個屬性值從x到y的動畫變化
-
1、定義動畫的主要步驟
- 建立一個動畫,並設定目標、屬性
- 設定屬性值的開始、插值、結束
- 動畫時長
- 啟動動畫
-
2、建構函式使用方式
- 1.
QPropertyAnimation(parent: QObject = None)
setTargetObject(self, QObject) setPropertyName(self, Union[QByteArray, bytes, bytearray])
- 2.
QPropertyAnimation(QObject, Union[QByteArray, bytes, bytearray], parent: QObject = None)
- 1.
-
3、常見的屬性
geometry pos size windowOpacity
-
4、設定開始值和結束值
setStartValue(self, Any) setEndValue(self, Any) setKeyValueAt(self, float, Any) setKeyValues(self, object)
-
5、設定動畫時長
-
setDuration(int mesc)
-
-
6、啟動動畫
-
start()
-
-
7、簡單案例(位置的)
import sys from PyQt5.Qt import * class Window(QWidget): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.setWindowTitle('動畫') self.resize(500, 500) self.move(400, 200) self.btn = QPushButton(self) self.init_ui() def init_ui(self): self.btn.resize(100, 100) self.btn.move(0, 0) self.btn.setStyleSheet('QPushButton{border: none; background: pink;}') # 1.定義一個動畫 animation = QPropertyAnimation(self) animation.setTargetObject(self.btn) animation.setPropertyName(b'pos') # 使用另外一種建構函式方式建立 # animation = QPropertyAnimation(self.btn, b'pos', self) # 2.設定屬性值 animation.setStartValue(QPoint(0, 0)) animation.setEndValue(QPoint(400, 400)) # 3.設定時長 animation.setDuration(3000) # 4.啟動動畫 animation.start() if __name__ == "__main__": app = QApplication(sys.argv) window = Window() window.show() sys.exit(app.exec_()) 複製程式碼
-
8、使用插值的動畫
import sys from PyQt5.Qt import * class Window(QWidget): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.setWindowTitle('使用插值') self.resize(500, 500) self.move(400, 200) self.btn = QPushButton(self) self.init_ui() def init_ui(self): self.btn.resize(50, 50) self.btn.move(0, 0) self.btn.setStyleSheet('QPushButton{border: none; background: pink;}') # 1.建立動畫 animation = QPropertyAnimation(self.btn, b'pos', self) # 2.定義動畫插值 animation.setKeyValueAt(0, QPoint(0, 0)) animation.setKeyValueAt(0.25, QPoint(450, 0)) animation.setKeyValueAt(0.5, QPoint(450, 450)) animation.setKeyValueAt(0.75, QPoint(0, 450)) animation.setKeyValueAt(1, QPoint(0, 0)) # 3.動畫時長 animation.setDuration(5000) # 4.啟動動畫 animation.start() if __name__ == "__main__": app = QApplication(sys.argv) window = Window() window.show() sys.exit(app.exec_()) 複製程式碼
四、 QAnimationGroup
動畫組的使用
可以將一組動畫, 同時播放或者按順序播放
-
1、使用的步驟
- 根據上面的方式建立單獨的動畫(但不啟動)
- 定義一個動畫組
- 將之前的動畫新增到動畫組中
- 啟動動畫組
-
2、動畫執行幾種狀態
QParallelAnimationGroup QSequentialAnimationGroup
-
3、一個動畫組的案例
import sys from PyQt5.Qt import * class Window(QWidget): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.setWindowTitle('動畫組') self.resize(500, 500) self.move(400, 200) self.btn1 = QPushButton(self) self.btn2 = QPushButton(self) self.init_ui() def init_ui(self): self.btn1.resize(50, 50) self.btn1.move(0, 0) self.btn1.setStyleSheet('QPushButton{border: none; background: pink;}') self.btn2.resize(50, 50) self.btn2.move(50, 50) self.btn2.setStyleSheet('border: none; background: cyan') # 按鈕1的動畫 animation1 = QPropertyAnimation(self.btn1, b'pos', self) animation1.setKeyValueAt(0, QPoint(0, 0)) animation1.setKeyValueAt(0.25, QPoint(450, 0)) animation1.setKeyValueAt(0.5, QPoint(450, 450)) animation1.setKeyValueAt(0.75, QPoint(0, 450)) animation1.setKeyValueAt(1, QPoint(0, 0)) animation1.setDuration(5000) # animation1.start() # 按鈕2的動畫 animation2 = QPropertyAnimation(self.btn2, b'pos', self) animation2.setKeyValueAt(0, QPoint(50, 50)) animation2.setKeyValueAt(0.25, QPoint(400, 50)) animation2.setKeyValueAt(0.5, QPoint(400, 400)) animation2.setKeyValueAt(0.75, QPoint(50, 400)) animation2.setKeyValueAt(1, QPoint(50, 50)) animation2.setDuration(3000) # animation2.start() animation_group = QSequentialAnimationGroup(self) animation_group.addAnimation(animation1) animation_group.addAnimation(animation2) animation_group.start() if __name__ == "__main__": app = QApplication(sys.argv) window = Window() window.show() sys.exit(app.exec_()) 複製程式碼