天天看点

PyQt5不规则窗体PyQt5不规则窗体

PyQt5不规则窗体

直接这么写有坑

mask_img = QImage(mask_filename)
self.setMask(QBitmap(mask_img))
           

核心在于

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

打个广告:编程代做,有需要的可以联系,QQ1692303843

全部代码如下

import sys
from PyQt5 import QtCore
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QWidget, QApplication, QDesktopWidget
from PyQt5.QtGui import QPixmap, QPainter


class LaunchPage(QWidget):
    """不规则窗体类"""
    def __init__(self):
        super(LaunchPage, self).__init__()
 
        self.pix = QPixmap('./images/xxx.png')  # 蒙版+图片
        self.resize(self.pix.size())
        self.setMask(self.pix.mask())
        # 设置无边框和置顶窗口样式
        self.setWindowFlags(Qt.FramelessWindowHint | QtCore.Qt.WindowStaysOnTopHint)
        self.center()
        
        
    def paintEvent(self, event):
        """绘制窗口"""
        paint = QPainter(self)
        paint.drawPixmap(0, 0, self.pix.width(), self.pix.height(), self.pix)
        
    def center(self):
        """实现窗体在屏幕中央"""
        # QDesktopWidget为一个类,调用screenGeometry函数获得屏幕的尺寸
        screen = QDesktopWidget().screenGeometry()
        size = self.geometry()
        x = (screen.width()-size.width()) / 2
        y = (screen.height()-size.height()) / 2
        self.move(int(x), int(y)) #调用move移动到指定位置


if __name__ == '__main__':
    app = QApplication(sys.argv)
    win = LaunchPage()
    win.show()
    sys.exit(app.exec_())