首页 > 解决方案 > 不透明时显示一次后无法设置 QWindow 的透明度

问题描述

我有一个窗口在不透明的情况下显示一次后试图使其透明。但是,即使我再次隐藏和显示也不会透明。这就是我得到的:

黑色背景

但我想要这样:(背景透明)

透明背景

如果我WA_TranslucentBackground先运行, .show()它按预期创建一个漂亮的透明按钮之前。

这是一个示例代码,当您运行它时会显示一个浮动按钮

import sys
from PySide2 import QtCore, QtWidgets, QtGui

class TransparentWindow(QtWidgets.QMainWindow):
    def __init__(self):
        super(TransparentWindow, self).__init__()
        self.setCentralWidget(QtWidgets.QWidget(self))
        self.mainLayout = QtWidgets.QVBoxLayout()
        self.centralWidget().setLayout(self.mainLayout)
        self.mainLayout.addWidget(QtWidgets.QPushButton("Hello World", parent=self))
        self.setWindowFlags(self.windowFlags() | QtCore.Qt.FramelessWindowHint)

        self.show() # If I move this under '.setAttribute()' it works
        self.setAttribute(QtCore.Qt.WA_TranslucentBackground)


if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    demo = TransparentWindow()
    sys.exit(app.exec_())

我试过隐藏和重新显示“setAutoFillBackground()”和“WA_NoSystemBackground”属性,但我似乎无法让它工作。有人知道如何在显示为不透明后设置透明度吗?

使用版本 QtCore:5.9.6,Pyside2:5.9.0a1.dev1528389443

标签: pythonpyside2

解决方案


推荐阅读