PyQt5 不规则窗口的显示

PyQt5不规则窗口的显示

  • QPixmap和QBitmap绘图的效果对比
  • 不可以拖动的不规则窗口
  • 可以拖动的不规则窗口
  • 不规则窗口实现动画效果
  • 加载GIF动画效果
函数描述
setMask(self,QBitmap)
setMask(self,QRegion)
setMask()的作用是为调用它的控件增加一个遮罩遮住所选区域以外的部分使之看起来是透明的。它的参数可以为QBitmap或QRegion对象。
paintEvent(self,QPaintEvent)通过重载paintEvent()函数绘制窗口背景

QPixmap和QBitmap绘图的效果对比

importsysfromPyQt5.QtWidgetsimportQApplication,QWidgetfromPyQt5.QtGuiimportQPixmap,QPainter,QBitmapclassMyForm(QWidget):def__init__(self,parentNone):super(MyForm,self).__init__(parent)self.setWindowTitle(不规则窗口的实现例子)defpaintEvent(self,event):painterQPainter(self)painter.drawPixmap(0,0,280,390,QPixmap(r./pyqt5/images/dog.jpg))painter.drawPixmap(300,0,280,390,QBitmap(r./pyqt5/images/dog.jpg))if__name____main__:appQApplication(sys.argv)formMyForm()form.show()sys.exit(app.exec_())

不可以拖动的不规则窗口

importsysfromPyQt5.QtWidgetsimportQApplication,QWidgetfromPyQt5.QtGuiimportQPixmap,QPainter,QBitmapclassWinForm(QWidget):def__init__(self,parentNone):super(WinForm,self).__init__(parent)self.setWindowTitle(不规则窗口的实现例子)self.pixQBitmap(./pyqt5/images/mask.png)self.resize(self.pix.size())self.setMask(self.pix)defpaintEvent(self,event):painterQPainter(self)#在指定区域直接绘制窗口背景painter.drawPixmap(0,0,self.pix.width(),self.pix.height(),QPixmap(./pyqt5/images/screen1.jpg))if__name____main__:appQApplication(sys.argv)winWinForm()win.show()sys.exit(app.exec_())

可以拖动的不规则窗口

importsysfromPyQt5.QtWidgetsimportQApplication,QWidgetfromPyQt5.QtGuiimportQPixmap,QPainter,QCursor,QBitmapfromPyQt5.QtCoreimportQtclassShapeWidget(QWidget):def__init__(self,parentNone):super(ShapeWidget,self).__init__(parent)self.setWindowTitle(不规则的可以拖动的窗体实现例子)self.mypix()#显示不规则picdefmypix(self):self.pixQBitmap(./pyqt5/images/mask.png)self.resize(self.pix.size())self.setMask(self.pix)print(self.pix.size())self.dragPositionNone#重定义鼠标按下响应函数mousePressEvent(QMouseEvent)和鼠标移动响应函数mouseMoveEvent(QMouseEvent)使不规则窗体能响应鼠标事件随意拖动。defmousePressEvent(self,event):ifevent.button()Qt.LeftButton:self.m_dragTrueself.m_DragPositionevent.globalPos()-self.pos()event.accept()self.setCursor(QCursor(Qt.OpenHandCursor))ifevent.button()Qt.RightButton:self.close()defmouseMoveEvent(self,QMouseEvent):ifQt.LeftButtonandself.m_drag:#当左键移动窗体修改偏移值self.move(QMouseEvent.globalPos()-self.m_DragPosition)QMouseEvent.accept()defmouseReleaseEvent(self,QMouseEvent):self.m_dragFalseself.setCursor(QCursor(Qt.ArrowCursor))#一般paintEvent在窗体首次绘制加载要重新加载paintEvent需要重新加载窗口使用self.update()orself.repaint()defpaintEvent(self,event):painterQPainter(self)painter.drawPixmap(0,0,self.width(),self.height(),QPixmap(./pyqt5/images/boy.png))if__name____main__:appQApplication(sys.argv)formShapeWidget()form.show()app.exec_()

不规则窗口实现动画效果

importsysfromPyQt5.QtWidgetsimportQApplication,QWidgetfromPyQt5.QtGuiimportQPixmap,QPainter,QCursorfromPyQt5.QtCoreimportQt,QTimerclassShapeWidget(QWidget):def__init__(self,parentNone):super(ShapeWidget,self).__init__(parent)self.i1self.mypix()self.timerQTimer()self.timer.setInterval(500)#定时器每500毫秒更新一次self.timer.timeout.connect(self.timeChange)self.timer.start()#显示不规则图片defmypix(self):self.update()ifself.i5:self.i1self.mypic{1:./pyqt5/images/left.png,2:./pyqt5/images/up.png,3:./pyqt5/images/right.png,4:./pyqt5/images/down.png}self.pixQPixmap(self.mypic[self.i],0,Qt.AvoidDither|Qt.ThresholdDither|Qt.ThresholdAlphaDither)self.resize(self.pix.size())self.setMask(self.pix.mask())self.dragPositionNonedefmousePressEvent(self,event):ifevent.button()Qt.LeftButton:self.m_dragTrueself.m_DragPositionevent.globalPos()-self.pos()event.accept()self.setCursor(QCursor(Qt.OpenHandCursor))defmouseMoveEvent(self,QMouseEvent):ifQt.LeftButtonandself.m_drag:self.move(QMouseEvent.globalPos()-self.m_DragPosition)QMouseEvent.accept()defmouseReleaseEvent(self,QMouseEvent):self.m_dragFalseself.setCursor(QCursor(Qt.ArrowCursor))defpaintEvent(self,event):painterQPainter(self)painter.drawPixmap(0,0,self.pix.width(),self.pix.height(),self.pix)defmouseDoubleClickEvent(self,event):ifevent.button()1:self.i1self.mypix()deftimeChange(self):self.i1self.mypix()if__name____main__:appQApplication(sys.argv)winShapeWidget()win.show()sys.exit(app.exec_())

加载GIF动画效果

importsysfromPyQt5.QtWidgetsimportQApplication,QLabel,QWidgetfromPyQt5.QtCoreimportQtfromPyQt5.QtGuiimportQMovieclassLoadingGifWin(QWidget):def__init__(self,parentNone):super(LoadingGifWin,self).__init__(parent)self.labelQLabel(,self)self.setFixedSize(128,128)self.setWindowFlags(Qt.Dialog|Qt.CustomizeWindowHint)self.movieQMovie(./pyqt5/images/loading.gif)self.label.setMovie(self.movie)self.movie.start()if__name____main__:appQApplication(sys.argv)winLoadingGifWin()win.show()sys.exit(app.exec_())

相关文章