简体   繁体   English

PyQt5:使用不透明小部件创建透明 window

[英]PyQt5: Create transparent window with opaque widgets

Is it possible to make mainWindow completely transparent while other widgets remains visible?是否可以使 mainWindow 完全透明而其他小部件仍然可见?

For example:例如:

I want to make app transparent and make everything else visible (like, mainFrame, close button, minimize button)我想让应用程序透明并使其他所有内容可见(例如 mainFrame、关闭按钮、最小化按钮)

As @Felipe mentioned you can use window.setAttribute(QtCore.Qt.WA_TranslucentBackground) here is a little example:正如@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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM