【问题标题】:PyQt5: Create transparent window with opaque widgetsPyQt5:使用不透明的小部件创建透明窗口
【发布时间】:2020-09-12 17:54:18
【问题描述】:

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

例如:

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

【问题讨论】:

标签: python python-3.x pyqt pyqt4


【解决方案1】:

正如@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()

【讨论】:

    猜你喜欢
    • 2016-03-03
    • 1970-01-01
    • 2018-08-12
    • 2010-10-21
    • 1970-01-01
    • 2019-04-24
    • 1970-01-01
    • 2013-02-21
    • 2021-06-24
    相关资源
    最近更新 更多