我有一个窗口,在不透明显示一次后,我试图使其透明。但是,即使我再次隐藏并显示,它也不会透明。这就是我得到的:
但是我想要这样:(背景透明)
如果我先运行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