pyqt5中動(dòng)畫(huà)的使用詳解
一、pyqt5中動(dòng)畫(huà)的繼承關(guān)系圖

二、關(guān)于QAbstractAnimation父類的認(rèn)識(shí)
1、主要作用
- 繼承此類, 實(shí)現(xiàn)一些自定義動(dòng)畫(huà)
- 所有動(dòng)畫(huà)共享的功能
2、功能作用
循環(huán)操作
- setLoopCount(count):設(shè)置循環(huán)次數(shù)
- currentLoop():當(dāng)前循環(huán)
- currentLoopTime():當(dāng)前循環(huán)時(shí)間
時(shí)間操作
- duration():單次時(shí)長(zhǎng)
- totalDuration():動(dòng)畫(huà)總時(shí)長(zhǎng)
- currentTime():當(dāng)前時(shí)長(zhǎng)
動(dòng)畫(huà)方向
- setDirection(QAbstractAnimation.Forward/QAbstractAnimation.Backward)
動(dòng)畫(huà)狀態(tài)state()
- QAbstractAnimation.Stopped:動(dòng)畫(huà)停止
- QAbstractAnimation.Paused:動(dòng)畫(huà)暫停
- QAbstractAnimation.Running:動(dòng)畫(huà)運(yùn)行
三、QPropertyAnimation屬性動(dòng)畫(huà)的使用
主要用于實(shí)現(xiàn)某個(gè)屬性值從x到y(tǒng)的動(dòng)畫(huà)變化
1、定義動(dòng)畫(huà)的主要步驟
- 創(chuàng)建一個(gè)動(dòng)畫(huà),并設(shè)置目標(biāo)、屬性
- 設(shè)置屬性值的開(kāi)始、插值、結(jié)束
- 動(dòng)畫(huà)時(shí)長(zhǎng)
- 啟動(dòng)動(dòng)畫(huà)
2、構(gòu)造函數(shù)使用方式
1.QPropertyAnimation(parent: QObject = None)
- 設(shè)置動(dòng)畫(huà)目標(biāo):setTargetObject(self, QObject)
- 設(shè)置動(dòng)畫(huà)屬性(位置、大小等):setPropertyName(self, Union[QByteArray, bytes, bytearray])
2.QPropertyAnimation(QObject, Union[QByteArray, bytes, bytearray], parent: QObject = None)
3、常見(jiàn)的屬性
- geometry
- pos
- size
- windowOpacity
4、設(shè)置開(kāi)始值和結(jié)束值
- setStartValue(self, Any)
- setEndValue(self, Any)
- setKeyValueAt(self, float, Any)
- setKeyValues(self, object)
5、設(shè)置動(dòng)畫(huà)時(shí)長(zhǎng)
- setDuration(int mesc)
6、啟動(dòng)動(dòng)畫(huà)
- start()
7、簡(jiǎn)單案例(位置的)
import sys
from PyQt5.Qt import *
class Window(QWidget):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.setWindowTitle('動(dòng)畫(huà)')
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.定義一個(gè)動(dòng)畫(huà)
animation = QPropertyAnimation(self)
animation.setTargetObject(self.btn)
animation.setPropertyName(b'pos')
# 使用另外一種構(gòu)造函數(shù)方式創(chuàng)建
# animation = QPropertyAnimation(self.btn, b'pos', self)
# 2.設(shè)置屬性值
animation.setStartValue(QPoint(0, 0))
animation.setEndValue(QPoint(400, 400))
# 3.設(shè)置時(shí)長(zhǎng)
animation.setDuration(3000)
# 4.啟動(dòng)動(dòng)畫(huà)
animation.start()
if __name__ == "__main__":
app = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())
8、使用插值的動(dòng)畫(huà)
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.創(chuàng)建動(dòng)畫(huà)
animation = QPropertyAnimation(self.btn, b'pos', self)
# 2.定義動(dòng)畫(huà)插值
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.動(dòng)畫(huà)時(shí)長(zhǎng)
animation.setDuration(5000)
# 4.啟動(dòng)動(dòng)畫(huà)
animation.start()
if __name__ == "__main__":
app = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())
四、QAnimationGroup動(dòng)畫(huà)組的使用
可以將一組動(dòng)畫(huà), 同時(shí)播放或者按順序播放
1、使用的步驟
- 根據(jù)上面的方式創(chuàng)建單獨(dú)的動(dòng)畫(huà)(但不啟動(dòng))
- 定義一個(gè)動(dòng)畫(huà)組
- 將之前的動(dòng)畫(huà)添加到動(dòng)畫(huà)組中
- 啟動(dòng)動(dòng)畫(huà)組
2、動(dòng)畫(huà)運(yùn)行幾種狀態(tài)
- 并行動(dòng)畫(huà)QParallelAnimationGroup
- 串行動(dòng)畫(huà)QSequentialAnimationGroup
3、一個(gè)動(dòng)畫(huà)組的案例
import sys
from PyQt5.Qt import *
class Window(QWidget):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.setWindowTitle('動(dòng)畫(huà)組')
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的動(dòng)畫(huà)
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的動(dòng)畫(huà)
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_())
五、關(guān)于QAbstractAnimation中事件的操作
1、啟動(dòng)動(dòng)畫(huà)start()
2、暫停動(dòng)畫(huà)pause()
3、繼續(xù)啟動(dòng)動(dòng)畫(huà)resume()
4、停止動(dòng)畫(huà)stop()
5、基本案例
import sys
from PyQt5.Qt import *
class Window(QWidget):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.setWindowTitle('動(dòng)畫(huà)組')
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的動(dòng)畫(huà)
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的動(dòng)畫(huà)
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(8000)
# animation2.start()
animation_group = QParallelAnimationGroup(self)
animation_group.addAnimation(animation1)
animation_group.addAnimation(animation2)
animation_group.start()
self.btn1.clicked.connect(animation_group.pause)
self.btn2.clicked.connect(animation_group.resume)
if __name__ == "__main__":
app = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())
到此這篇關(guān)于pyqt5中動(dòng)畫(huà)的使用詳解的文章就介紹到這了,更多相關(guān)pyqt5 動(dòng)畫(huà)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
django前端頁(yè)面下拉選擇框默認(rèn)值設(shè)置方式
這篇文章主要介紹了django前端頁(yè)面下拉選擇框默認(rèn)值設(shè)置方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-08-08
Python3訪問(wèn)并下載網(wǎng)頁(yè)內(nèi)容的方法
這篇文章主要介紹了Python3訪問(wèn)并下載網(wǎng)頁(yè)內(nèi)容的方法,實(shí)例分析了Python頁(yè)面抓取及寫(xiě)入文件的實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-07-07
在CMD窗口中調(diào)用python函數(shù)的實(shí)現(xiàn)
本文主要介紹了在CMD窗口中調(diào)用python函數(shù)的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07
Windows系統(tǒng)配置python腳本開(kāi)機(jī)啟動(dòng)的3種方法分享
這篇文章主要介紹了Windows系統(tǒng)配置python腳本開(kāi)機(jī)啟動(dòng)的3種方法分享,本文講解了開(kāi)始菜單啟動(dòng)項(xiàng)實(shí)現(xiàn)、開(kāi)機(jī)腳本、通過(guò)一個(gè)服務(wù)調(diào)用該腳本三種方法,需要的朋友可以參考下2015-03-03
Python用20行代碼實(shí)現(xiàn)完整郵件功能
這篇文章主要介紹了如何使用Python實(shí)現(xiàn)完整郵件功能的相關(guān)資料,需要的朋友可以參考下面文章內(nèi)容,希望能幫助到您2021-09-09
Python中集合的內(nèi)建函數(shù)和內(nèi)建方法學(xué)習(xí)教程
這篇文章主要介紹了Python中集合的內(nèi)建函數(shù)和內(nèi)建方法學(xué)習(xí)教程,包括工廠函數(shù)和僅用于可變集合的方法等知識(shí)點(diǎn),需要的朋友可以參考下2015-08-08
pandas刪除行刪除列增加行增加列的實(shí)現(xiàn)
這篇文章主要介紹了pandas刪除行刪除列增加行增加列的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07

