1. 程式人生 > 程式設計 >python GUI庫圖形介面開發之PyQt5不規則視窗實現與顯示GIF動畫的詳細方法與例項

python GUI庫圖形介面開發之PyQt5不規則視窗實現與顯示GIF動畫的詳細方法與例項

PyQt5不規則視窗實現動畫效果例項

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()
  #顯示不規則圖片
  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,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_())

執行程式,效果如下

python GUI庫圖形介面開發之PyQt5不規則視窗實現與顯示GIF動畫的詳細方法與例項python GUI庫圖形介面開發之PyQt5不規則視窗實現與顯示GIF動畫的詳細方法與例項python GUI庫圖形介面開發之PyQt5不規則視窗實現與顯示GIF動畫的詳細方法與例項

程式碼分析

執行這個例子,會彈出一個視窗,顯示不同方向的箭頭,每0.5秒改變一次方向

pixmap.setMask()函式的作用是為呼叫它的控制元件增加一個遮罩,遮住所選區域以外的地方,使控制元件看起來是透明的,它的引數是一個QBitmap物件或一個QRegion物件

本例中呼叫QPixmap例項的self.pix.mask()函式獲得圖片自身的遮罩,這個遮罩是一個QBitmap物件

 self.pix=QPixmap(self.mypic[self.i],Qt.AvoidDither|Qt.ThresholdAlphaDither|Qt.ThresholdDither)

        self.resize(self.pix.size())

        self.setMask(self.pix.mask())

paintEvent()函式每次初始化視窗時只調用一次,所以沒載入一次圖片就要重新呼叫一次paintEvent()函式,即在更新視窗時呼叫這個函式,更新視窗的核心程式碼如下

        self.timer=QTimer()

        self.timer.setInterval(500)

        self.timer.timeout.connect(self.timeChanged)

        self.timer.start()

當定時器的時間到期後更新視窗程式碼

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)
    #例項化標籤到視窗中
    self.label=QLabel('',self)
    #設定標籤的寬度與高度
    self.setFixedSize(128,128)
    #設定無邊框
    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_())

執行效果

python GUI庫圖形介面開發之PyQt5不規則視窗實現與顯示GIF動畫的詳細方法與例項

本文主要講解了PyQt5實現視窗動畫的兩種方法,推薦第2種PyQt5載入顯示GIF動畫方法,想了解更多關於PyQt5視窗知識請檢視下面的相關連結