pytho中GUI库图形界面开发之PyQt5不规则窗口实现与显示GIF动画的示例分析

  介绍

小编给大家分享一下pytho中GUI库图形界面开发之PyQt5不规则窗口实现与显示GIF动画的示例分析,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获、下面让我们一起去了解一下吧!

PyQt5不规则窗口实现动画效果实例

import 系统   得到PyQt5.QtCore  import  *   得到PyQt5.QtGui  import  *   得到PyQt5.QtWidgets  import  *   class  ShapeWidget (QWidget):   def 才能;__init__(自我,父=None):   ,,,超级(ShapeWidget,,自我). __init__(父)   ,,,意念=1   ,,,self.mypix ()   ,,,self.timer=QTimer ()   ,,,self.timer.setInterval (500)   ,,,self.timer.timeout.connect (self.timeChanged)   ,,,self.timer.start ()   #才能显示不规则图片   def 才能mypix(自我):   ,,,self.update ()   ,,,if 意念的==5:   ,,,,,意念=1   ,,,self.mypic={1: & # 39;。/图片/left.png& # 39;, 2: & # 39;。/图片/up.png& # 39;, 3: & # 39;。/图片/right.png& # 39;, 4: & # 39;。/图片/down.png& # 39;}   ,,,self.pix=QPixmap (self.mypic(意念)& # 39;0 & # 39;,Qt.AvoidDither | Qt.ThresholdAlphaDither | Qt.ThresholdDither)   ,,,self.resize (self.pix.size ())   ,,,self.setMask (self.pix.mask ())   ,,,self.dragPosition=没有   def 才能;mousePressEvent(自我,,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 才能摇干(自我,QMouseEvent):   ,,,if  Qt.LeftButton 以及self.m_drag:   ,,,,,self.move (QMouseEvent.globalPos () -self.m_DragPosition)   ,,,,,QMouseEvent.accept ()   def 才能;mouseReleaseEvent(自我,,QMouseEvent):   ,,self.m_drag=False   ,,,self.setCursor (QCursor (Qt.ArrowCursor))   def 才能;paintEvent(自我,,QPaintEvent):   ,,,画家=QPainter(自我)   ,,,painter.drawPixmap (0, 0, self.pix.width (), self.pix.height (), self.pix)   def 才能;mouseDoubleClickEvent(自我,,QMouseEvent):   ,,,if  QMouseEvent.button ()==1:   ,,,,,意念+=1   ,,,,,self.mypix ()   def 才能timeChanged(自我):   ,,,意念+=1   ,,,self.mypix ()   if  __name__ ==, & # 39; __main__ # 39;:   应用=才能QApplication (sys.argv)   形式才能=ShapeWidget ()   form.show才能()   sys.exit才能(app.exec_ ())

运行程序,效果如下

 pytho中GUI库图形界面开发之PyQt5不规则窗口实现与显示GIF动画的示例分析“> <img src=

代码分析

运行这个例子,会弹出一个窗口,显示不同方向的箭头,每0.5秒改变一次方向

pixmap.setMask()函数的作用是为调用它的控件增加一个遮罩,遮住所选区域以外的地方,使控件看起来是透明的,它的参数是一个QBitmap对象或一个QRegion对象

本例中调用QPixmap实例的self.pix.mask()函数获得图片自身的遮罩,这个遮罩是一个QBitmap对象

, self.pix=QPixmap (self.mypic(意念)& # 39;0 & # 39;,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 ()

当定时器的时间到期后更新窗口代码

自我。更新

PyQt5加载GIF动画实例

import 系统   得到PyQt5.QtCore  import  *   得到PyQt5.QtGui  import  *   得到PyQt5.QtWidgets  import  *   class  LoadingGifWin (QWidget):   def 才能;__init__(自我,父=None):   ,,,超级(LoadingGifWin,,自我). __init__(父)   ,,,#实例化标签到窗口中   ,,,self.label=QLabel(& # 39; & # 39;自我)   ,,,#设置标签的宽度与高度   ,,,self.setFixedSize (128128)   ,,,#设置无边框   ,,,self.setWindowFlags (Qt.Dialog  |, Qt.CustomizeWindowHint)   ,,,self.movie=QMovie(& # 39;。/图片/loading.gif& # 39;)   ,,,self.label.setMovie (self.movie)   null   null   null   null   null   null

pytho中GUI库图形界面开发之PyQt5不规则窗口实现与显示GIF动画的示例分析