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

是否可以使 mainWindow 完全透明而其他小部件保持可见?

例如:

http://img3.mukewang.com/638ee8250001bd7812810568.jpg

我想让应用程序透明并使其他所有内容可见(例如,mainFrame、关闭按钮、最小化按钮)



慕尼黑的夜晚无繁华
浏览 340回答 1
1回答

阿波罗的战车

您可以使用 window.setAttribute(QtCore.Qt.WA_TranslucentBackground) 这里是一个小例子:import sysfrom PyQt5 import QtWidgets, QtCoreapp = QtWidgets.QApplication(sys.argv)# create invisble widgetwindow = 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 invisiblevisible_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 buttonclose_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 transparentchange_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()
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python