python GUI庫圖形界面開發(fā)之PyQt5不規(guī)則窗口實(shí)現(xiàn)與顯示GIF動(dòng)畫的詳細(xì)方法與實(shí)例
PyQt5不規(guī)則窗口實(shí)現(xiàn)動(dòng)畫效果實(shí)例
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
class ShapeWidget(QWidget):
def __init__(self,parent=None):
super(ShapeWidget, self).__init__(parent)
self.i=1
self.mypix()
self.timer=QTimer()
self.timer.setInterval(500)
self.timer.timeout.connect(self.timeChanged)
self.timer.start()
#顯示不規(guī)則圖片
def mypix(self):
self.update()
if self.i==5:
self.i=1
self.mypic={1:'./images/left.png',2:'./images/up.png',3:'./images/right.png',4:'./images/down.png'}
self.pix=QPixmap(self.mypic[self.i],'0',Qt.AvoidDither|Qt.ThresholdAlphaDither|Qt.ThresholdDither)
self.resize(self.pix.size())
self.setMask(self.pix.mask())
self.dragPosition=None
def mousePressEvent(self, QMouseEvent):
if QMouseEvent.button()==Qt.LeftButton:
self.m_drag=True
self.m_DragPosition=QMouseEvent.globalPos()-self.pos()
QMouseEvent.accept()
self.setCursor(QCursor(Qt.OpenHandCursor))
def mouseMoveEvent(self, QMouseEvent):
if Qt.LeftButton and self.m_drag:
self.move(QMouseEvent.globalPos()-self.m_DragPosition)
QMouseEvent.accept()
def mouseReleaseEvent(self, QMouseEvent):
self.m_drag=False
self.setCursor(QCursor(Qt.ArrowCursor))
def paintEvent(self, QPaintEvent):
painter=QPainter(self)
painter.drawPixmap(0,0,self.pix.width(),self.pix.height(),self.pix)
def mouseDoubleClickEvent(self, QMouseEvent):
if QMouseEvent.button()==1:
self.i+=1
self.mypix()
def timeChanged(self):
self.i+=1
self.mypix()
if __name__ == '__main__':
app=QApplication(sys.argv)
form=ShapeWidget()
form.show()
sys.exit(app.exec_())
運(yùn)行程序,效果如下



代碼分析
運(yùn)行這個(gè)例子,會(huì)彈出一個(gè)窗口,顯示不同方向的箭頭,每0.5秒改變一次方向
pixmap.setMask()函數(shù)的作用是為調(diào)用它的控件增加一個(gè)遮罩,遮住所選區(qū)域以外的地方,使控件看起來是透明的,它的參數(shù)是一個(gè)QBitmap對(duì)象或一個(gè)QRegion對(duì)象
本例中調(diào)用QPixmap實(shí)例的self.pix.mask()函數(shù)獲得圖片自身的遮罩,這個(gè)遮罩是一個(gè)QBitmap對(duì)象
self.pix=QPixmap(self.mypic[self.i],'0',Qt.AvoidDither|Qt.ThresholdAlphaDither|Qt.ThresholdDither)
self.resize(self.pix.size())
self.setMask(self.pix.mask())
paintEvent()函數(shù)每次初始化窗口時(shí)只調(diào)用一次,所以沒加載一次圖片就要重新調(diào)用一次paintEvent()函數(shù),即在更新窗口時(shí)調(diào)用這個(gè)函數(shù),更新窗口的核心代碼如下
self.timer=QTimer()
self.timer.setInterval(500)
self.timer.timeout.connect(self.timeChanged)
self.timer.start()
當(dāng)定時(shí)器的時(shí)間到期后更新窗口代碼
self.update
PyQt5加載GIF動(dòng)畫實(shí)例
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
class LoadingGifWin(QWidget):
def __init__(self,parent=None):
super(LoadingGifWin, self).__init__(parent)
#實(shí)例化標(biāo)簽到窗口中
self.label=QLabel('',self)
#設(shè)置標(biāo)簽的寬度與高度
self.setFixedSize(128,128)
#設(shè)置無邊框
self.setWindowFlags(Qt.Dialog | Qt.CustomizeWindowHint)
self.movie=QMovie('./images/loading.gif')
self.label.setMovie(self.movie)
self.movie.start()
if __name__ == '__main__':
app=QApplication(sys.argv)
load=LoadingGifWin()
load.show()
sys.exit(app.exec_())
運(yùn)行效果

本文主要講解了PyQt5實(shí)現(xiàn)窗口動(dòng)畫的兩種方法,推薦第2種PyQt5加載顯示GIF動(dòng)畫方法,想了解更多關(guān)于PyQt5窗口知識(shí)請(qǐng)查看下面的相關(guān)鏈接
相關(guān)文章
使用pandas將numpy中的數(shù)組數(shù)據(jù)保存到csv文件的方法
今天小編就為大家分享一篇使用pandas將numpy中的數(shù)組數(shù)據(jù)保存到csv文件的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-06-06
Python編程中非常重要卻又被嚴(yán)重低估的庫decorator
今天介紹的是一個(gè)已經(jīng)存在十年,但是依舊不紅的庫 decorator,好像很少有人知道他的存在一樣。本篇文章不會(huì)過多的向你介紹裝飾器的基本知識(shí),我會(huì)默認(rèn)你知道什么是裝飾器,并且懂得如何寫一個(gè)簡單的裝飾器2021-10-10
Python網(wǎng)絡(luò)爬蟲之Web網(wǎng)頁基礎(chǔ)
我們?cè)趯W(xué)習(xí)爬蟲之前,要先了解網(wǎng)頁的組成,只有我們了解其組成嗎,才可以方能百戰(zhàn)百勝,文章中有詳細(xì)的代碼示例,需要的朋友可以參考一下2023-04-04
Python抓取京東圖書評(píng)論數(shù)據(jù)
最近接了個(gè)項(xiàng)目,需要抓取京東圖書的評(píng)論,把代碼放出來給大家分享下,希望能有所幫助2014-08-08
python gui開發(fā)——制作抖音無水印視頻下載工具(附源碼)
這篇文章主要介紹了python gui開發(fā)——制作抖音無水印視頻下載工具(附源碼)的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)使用python,感興趣的朋友可以了解下2021-02-02
如何使用五行Python代碼輕松實(shí)現(xiàn)批量摳圖
簡單來說,摳圖就是將照片的主體人或物品從圖片中摳出來,以便貼到別處使用,下面這篇文章主要給大家介紹了關(guān)于如何使用五行Python代碼輕松實(shí)現(xiàn)批量摳圖的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-04-04
Python調(diào)用Prometheus監(jiān)控?cái)?shù)據(jù)并計(jì)算
Prometheus是一套開源監(jiān)控系統(tǒng)和告警為一體,由go語言(golang)開發(fā),是監(jiān)控+報(bào)警+時(shí)間序列數(shù)據(jù)庫的組合。本文將介紹Python如何調(diào)用Prometheus實(shí)現(xiàn)數(shù)據(jù)的監(jiān)控與計(jì)算,需要的可以參考一下2021-12-12

