欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

PYQT5 自定义不规则窗体

程序员文章站 2022-05-11 08:09:36
...

代码:图片要求是一个png图片

import sys
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QWidget, QApplication
from PyQt5.QtGui import QPixmap, QPainter, QBitmap, QCursor
import PyQt5.QtCore as QtCore


class PixWindow(QWidget):  # 不规则窗体
    def __init__(self):
        super().__init__()

        self.pix = QPixmap('beijing.png')  # 蒙版
        windowWidth = 700
        windowHeight = 700
        self.resize(windowWidth, windowHeight)
        self.pix = self.pix.scaled(int(windowWidth), int(windowHeight))
        self.setMask(self.pix.mask())

        self.setWindowFlags(Qt.FramelessWindowHint | QtCore.Qt.WindowStaysOnTopHint)  # 设置无边框和置顶窗口样式

    def paintEvent(self, event):  # 绘制窗口
        paint = QPainter(self)
        paint.drawPixmap(0, 0, self.pix.width(), self.pix.height(), self.pix)


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

 

相关标签: pyqt