python GUI庫圖形界面開發(fā)之PyQt5不規(guī)則窗口實現(xiàn)與顯示GIF動畫的詳細(xì)方法與實例
PyQt5不規(guī)則窗口實現(xiàn)動畫效果實例
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_())
運行程序,效果如下
代碼分析
運行這個例子,會彈出一個窗口,顯示不同方向的箭頭,每0.5秒改變一次方向
pixmap.setMask()函數(shù)的作用是為調(diào)用它的控件增加一個遮罩,遮住所選區(qū)域以外的地方,使控件看起來是透明的,它的參數(shù)是一個QBitmap對象或一個QRegion對象
本例中調(diào)用QPixmap實例的self.pix.mask()函數(shù)獲得圖片自身的遮罩,這個遮罩是一個QBitmap對象
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ù)每次初始化窗口時只調(diào)用一次,所以沒加載一次圖片就要重新調(diào)用一次paintEvent()函數(shù),即在更新窗口時調(diào)用這個函數(shù),更新窗口的核心代碼如下
self.timer=QTimer()
self.timer.setInterval(500)
self.timer.timeout.connect(self.timeChanged)
self.timer.start()
當(dāng)定時器的時間到期后更新窗口代碼
self.update
PyQt5加載GIF動畫實例
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) #實例化標(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_())
運行效果
本文主要講解了PyQt5實現(xiàn)窗口動畫的兩種方法,推薦第2種PyQt5加載顯示GIF動畫方法,想了解更多關(guān)于PyQt5窗口知識請查看下面的相關(guān)鏈接
相關(guān)文章
Python多線程、異步+多進(jìn)程爬蟲實現(xiàn)代碼
這篇文章主要介紹了Python多線程、異步+多進(jìn)程爬蟲實現(xiàn)代碼,需要的朋友可以參考下2016-02-02python數(shù)字轉(zhuǎn)對應(yīng)中文的方法總結(jié)
在本篇文章里小編給大家分享的是一篇關(guān)于python數(shù)字轉(zhuǎn)對應(yīng)中文的方法總結(jié)內(nèi)容,有興趣的朋友們可以跟著猜嘗試測試下。2021-08-08解決Pytorch中Batch Normalization layer踩過的坑
這篇文章主要介紹了解決Pytorch中Batch Normalization layer踩過的坑,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-05-05Python實現(xiàn)讀取SQLServer數(shù)據(jù)并插入到MongoDB數(shù)據(jù)庫的方法示例
這篇文章主要介紹了Python實現(xiàn)讀取SQLServer數(shù)據(jù)并插入到MongoDB數(shù)據(jù)庫的方法,涉及Python同時進(jìn)行SQLServer與MongoDB數(shù)據(jù)庫的連接、查詢、讀取、寫入等相關(guān)操作實現(xiàn)技巧,需要的朋友可以參考下2018-06-06Empty test suite.(PyCharm程序運行錯誤的解決方法)
今天小編就為大家分享一篇Empty test suite.(PyCharm程序運行錯誤的解決方法),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-11-11