PyQt5: 用不透明的小部件创建透明窗口

问题描述 投票:0回答:1

是否可以让mainWindow完全透明,而其他小部件仍然可见?

例如:我想把应用程序变成透明的,而其他的部件都是可见的(比如mainFrame,关闭按钮,最小化按钮)。

我想把应用程序变成透明的,同时让其他所有的东西都是可见的(比如,主框架,关闭按钮,最小化按钮)。

python python-3.x pyqt pyqt4
1个回答
0
投票

正如 @Felipe 提到的,你可以使用 window.setAttribute(QtCore.Qt.WA_TranslucentBackground)这里有一个小例子。

import sys
from PyQt5 import QtWidgets, QtCore

app = QtWidgets.QApplication(sys.argv)

# create invisble widget
window = QtWidgets.QWidget()
window.setAttribute(QtCore.Qt.WA_TranslucentBackground)
window.setWindowFlags(QtCore.Qt.FramelessWindowHint)
window.setFixedSize(800, 600)

# add visible child widget, when this widget is transparent it will also be invisible
visible_child = QtWidgets.QWidget(window)
visible_child.setStyleSheet('QWidget{background-color: white}')
visible_child.setObjectName('vc')
visible_child.setFixedSize(800, 600)
layout = QtWidgets.QGridLayout()

# add a close button
close_button = QtWidgets.QPushButton()
close_button.setText('close window')
close_button.clicked.connect(lambda: app.exit(0))
layout.addWidget(close_button)

# add a button that makes the visible child widget transparent
change_size_button = QtWidgets.QPushButton()
change_size_button.setText('change size')
change_size_button.clicked.connect(lambda: visible_child.setStyleSheet('QWidget#vc{background-color: transparent}'))
layout.addWidget(change_size_button)

visible_child.setLayout(layout)
window.show()
app.exec()
© www.soinside.com 2019 - 2024. All rights reserved.