我是靠谱客的博主 还单身山水,这篇文章主要介绍PyQt6 制作半透明png背景的窗体,现在分享给大家,希望可以做个参考。

import sys,time
from PyQt6.QtCore import Qt
from PyQt6.QtGui import QImage
from PyQt6.QtWidgets import QApplication, QMainWindow

from PyQt6.QtGui import QPainter

class Img(QMainWindow):
    def __init__(self, img_path, parent=None):
        super().__init__(parent)
        self.qimg = QImage(img_path)
        self.setStyleSheet('QMainWindow {background:transparent}')
        self.setWindowFlags(
            Qt.WindowType.WindowStaysOnTopHint |
            Qt.WindowType.FramelessWindowHint |
            Qt.WindowType.WindowTransparentForInput
        )
        self.setAttribute(Qt.WidgetAttribute.WA_TranslucentBackground)
        
    def paintEvent(self, qpaint_event):
        painter = QPainter(self)
        rect = qpaint_event.rect()
        painter.drawImage(rect, self.qimg)
        #self.showFullScreen()
        painter.end()
        

app = QApplication([])

img_path = 'pyqt5/bg_1080.png'
window = Img(img_path)
#window.resize(1920,1080)
window.showFullScreen()

sys.exit(app.exec())

import sys,time
from PyQt6.QtCore import Qt
from PyQt6.QtGui import QImage
from PyQt6.QtWidgets import QApplication, QMainWindow

from PyQt6.QtGui import QPainter

class Img(QMainWindow):
    def __init__(self, img_path, parent=None):
        super().__init__(parent)
        self.qimg = QImage(img_path)
        self.setStyleSheet('QMainWindow {background:transparent}')
        self.setWindowFlags(
            Qt.WindowType.WindowStaysOnTopHint |
            Qt.WindowType.FramelessWindowHint |
            Qt.WindowType.WindowTransparentForInput
        )
        self.setAttribute(Qt.WidgetAttribute.WA_TranslucentBackground)
        
    def paintEvent(self, qpaint_event):
        painter = QPainter(self)
        rect = qpaint_event.rect()
        painter.drawImage(rect, self.qimg)
        #self.showFullScreen()
        painter.end()
        

app = QApplication([])

img_path = 'pyqt5/bg_1080.png'
window = Img(img_path)
#window.resize(1920,1080)
window.showFullScreen()

sys.exit(app.exec())

最后

以上就是还单身山水最近收集整理的关于PyQt6 制作半透明png背景的窗体的全部内容,更多相关PyQt6内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(178)

评论列表共有 0 条评论

立即
投稿
返回
顶部