国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術(shù)文章
文章詳情頁

python GUI庫圖形界面開發(fā)之PyQt5不規(guī)則窗口實現(xiàn)與顯示GIF動畫的詳細(xì)方法與實例

瀏覽:47日期:2022-08-03 11:29:34
PyQt5不規(guī)則窗口實現(xiàn)動畫效果實例

import sysfrom 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)行程序,效果如下

python GUI庫圖形界面開發(fā)之PyQt5不規(guī)則窗口實現(xiàn)與顯示GIF動畫的詳細(xì)方法與實例python GUI庫圖形界面開發(fā)之PyQt5不規(guī)則窗口實現(xiàn)與顯示GIF動畫的詳細(xì)方法與實例python GUI庫圖形界面開發(fā)之PyQt5不規(guī)則窗口實現(xiàn)與顯示GIF動畫的詳細(xì)方法與實例

代碼分析

運(yùn)行這個例子,會彈出一個窗口,顯示不同方向的箭頭,每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 sysfrom 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_())

運(yùn)行效果

python GUI庫圖形界面開發(fā)之PyQt5不規(guī)則窗口實現(xiàn)與顯示GIF動畫的詳細(xì)方法與實例

本文主要講解了PyQt5實現(xiàn)窗口動畫的兩種方法,推薦第2種PyQt5加載顯示GIF動畫方法,想了解更多關(guān)于PyQt5窗口知識請查看下面的相關(guān)鏈接

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 华容县| 朝阳县| 博白县| 柯坪县| 麦盖提县| 军事| 鄂托克旗| 江山市| 博爱县| 万源市| 卓尼县| 鄂托克前旗| 六枝特区| 临武县| 赣州市| 阳西县| 万盛区| 比如县| 巩义市| 绥江县| 辉县市| 杂多县| 文化| 温州市| 水富县| 渝北区| 涡阳县| 大竹县| 屏东市| 太仓市| 茂名市| 林西县| 建湖县| 曲阜市| 阿合奇县| 全南县| 黄骅市| 山阳县| 磐石市| 孝感市| 宜兰市|