Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PyQt5: Create transparent window with opaque widgets

Is it possible to make mainWindow completely transparent while other widgets remains visible?

For example:

I want to make app transparent and make everything else visible (like, mainFrame, close button, minimize button)

like image 924
rEb00t_1001 Avatar asked Mar 24 '26 14:03

rEb00t_1001


1 Answers

As @Felipe mentioned you can use window.setAttribute(QtCore.Qt.WA_TranslucentBackground) here is a little example:

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()
like image 96
MrShadowjockey Avatar answered Mar 26 '26 04:03

MrShadowjockey



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!