• PyQt5 不规则窗口的显示


    函数描述
    setMask(self, QBitmap)
    setMask(self, QRegion)
    setMask()的作用是为调用它的控件增加一个遮罩,遮住所选区域以外的部分,使之看起来是透明的。它的参数可以为QBitmap或QRegion对象。
    paintEvent(self, QPaintEvent)通过重载paintEvent()函数绘制窗口背景

    QPixmap和QBitmap绘图的效果对比

    import sys
    from PyQt5.QtWidgets import QApplication, QWidget
    from PyQt5.QtGui import QPixmap, QPainter, QBitmap
    
    class MyForm(QWidget):
        def __init__(self, parent=None):
            super(MyForm, self).__init__(parent)
            self.setWindowTitle("不规则窗口的实现例子")
    
        def paintEvent(self, event):
            painter = QPainter(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__":
        app = QApplication(sys.argv)
        form = MyForm()
        form.show()
        sys.exit(app.exec_())
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    在这里插入图片描述

    不可以拖动的不规则窗口

    import sys
    from PyQt5.QtWidgets import QApplication, QWidget
    from PyQt5.QtGui import QPixmap, QPainter, QBitmap
    
    class WinForm(QWidget):
        def __init__(self, parent=None):
            super(WinForm, self).__init__(parent)
            self.setWindowTitle("不规则窗口的实现例子")
    
            self.pix = QBitmap("./pyqt5/images/mask.png")
            self.resize(self.pix.size())
            self.setMask(self.pix)
    
        def paintEvent(self, event):
            painter = QPainter(self)
            # 在指定区域直接绘制窗口背景
            painter.drawPixmap(0, 0, self.pix.width(), self.pix.height(), QPixmap("./pyqt5/images/screen1.jpg"))
    
    if __name__ == "__main__":
        app = QApplication(sys.argv)
        win = WinForm()
        win.show()
        sys.exit(app.exec_())
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    在这里插入图片描述

    可以拖动的不规则窗口

    import sys
    from PyQt5.QtWidgets import QApplication  ,QWidget 
    from PyQt5.QtGui import  QPixmap,   QPainter  ,  QCursor , QBitmap
    from PyQt5.QtCore import Qt 
    
    class ShapeWidget(QWidget):  
    	def __init__(self,parent=None):  
    		super(ShapeWidget,self).__init__(parent)
    		self.setWindowTitle("不规则的,可以拖动的窗体实现例子") 
    		self.mypix()	
    
        # 显示不规则 pic
    	def mypix(self):
    		self.pix = QBitmap( "./pyqt5/images/mask.png" )
    		self.resize(self.pix.size())       
    		self.setMask(self.pix)
    		print( self.pix.size())
    		self.dragPosition = None
    
    	# 重定义鼠标按下响应函数mousePressEvent(QMouseEvent)和鼠标移动响应函数mouseMoveEvent(QMouseEvent),使不规则窗体能响应鼠标事件,随意拖动。
    	def mousePressEvent(self, event):
    		if event.button() == Qt.LeftButton:
    			self.m_drag=True
    			self.m_DragPosition=event.globalPos()-self.pos()
    			event.accept()
    			self.setCursor(QCursor(Qt.OpenHandCursor))
    		if event.button()==Qt.RightButton:  
    			self.close()  
    			
    	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))
        
        #一般 paintEvent 在窗体首次绘制加载, 要重新加载paintEvent 需要重新加载窗口使用 self.update() or  self.repaint()    
    	def paintEvent(self, event):
    		painter = QPainter(self)
    		painter.drawPixmap(0,0,self.width(),self.height(),QPixmap("./pyqt5/images/boy.png"))
    			
    if __name__ == '__main__':
        app=QApplication(sys.argv)
        form=ShapeWidget()
        form.show()
        app.exec_()
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50

    在这里插入图片描述

    不规则窗口实现动画效果

    import sys
    from PyQt5.QtWidgets import QApplication, QWidget
    from PyQt5.QtGui import QPixmap, QPainter, QCursor
    from PyQt5.QtCore import Qt, QTimer
    
    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) # 定时器每500毫秒更新一次
            self.timer.timeout.connect(self.timeChange)
            self.timer.start()
    
        # 显示不规则图片
        def mypix(self):
            self.update()
            if self.i == 5:
                self.i = 1
            self.mypic = {1:'./pyqt5/images/left.png', 2:'./pyqt5/images/up.png', 3:'./pyqt5/images/right.png', 4:'./pyqt5/images/down.png'}
            self.pix = QPixmap(self.mypic[self.i], "0", Qt.AvoidDither | Qt.ThresholdDither | Qt.ThresholdAlphaDither)
            self.resize(self.pix.size())
            self.setMask(self.pix.mask())
            self.dragPosition = None
    
        def mousePressEvent(self, event):
            if event.button() == Qt.LeftButton:
                self.m_drag = True
                self.m_DragPosition = event.globalPos() - self.pos()
                event.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, event):
            painter = QPainter(self)
            painter.drawPixmap(0, 0, self.pix.width(), self.pix.height(), self.pix)
    
        def mouseDoubleClickEvent(self, event):
            if event.button() == 1:
                self.i += 1
                self.mypix()
    
        def timeChange(self):
            self.i += 1
            self.mypix()
    
    if __name__ == "__main__":
        app = QApplication(sys.argv)
        win = ShapeWidget()
        win.show()
        sys.exit(app.exec_())
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61

    在这里插入图片描述

    加载GIF动画效果

    import sys
    from PyQt5.QtWidgets import QApplication, QLabel, QWidget
    from PyQt5.QtCore import Qt
    from PyQt5.QtGui import QMovie
    
    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("./pyqt5/images/loading.gif")
            self.label.setMovie(self.movie)
            self.movie.start()
            
    if __name__ == "__main__":
        app = QApplication(sys.argv)
        win = LoadingGifWin()
        win.show()
        sys.exit(app.exec_())
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    在这里插入图片描述

  • 相关阅读:
    springBoot全局异常定义
    Linux常用初级指令介绍和使用
    SOFAJRaft 日志复制共识算法
    openGauss Meetup(杭州站)全程精彩回顾
    京东数据分析:2023年9月京东白酒行业品牌销售排行榜
    CyberDAO:web3时代的引领者
    【面试题】LockSupport
    Linux每日智囊
    2022年这一批陕西省工程职称评审难度调整了
    每秒生成10万订单号
  • 原文地址:https://blog.csdn.net/u013420428/article/details/128129396