I want to have the following: a PyQt application with a transparent background, but with the “frame” still visible. With frame I mean a border which you can use to resize, the titlebar and also the close and minimize button.
Here’s what I tried:
import sys
from PyQt5.QtWidgets import (
QWidget, QApplication, QPushButton, QVBoxLayout,
QLabel
)
from PyQt5.QtGui import (
QPainter, QBrush, QColor, QPen
)
from PyQt5.QtCore import QRect, QPoint, Qt, QSize
#%%
class MyWidget(QWidget):
def __init__(self):
super().__init__()
self.setGeometry(30,30,600,400)
# These lines try to make the background transparent
self.setWindowOpacity(.8)
self.setStyleSheet("background:red;")
self.setAttribute(Qt.WA_NoSystemBackground)
self.setAttribute(Qt.WA_TranslucentBackground)
self.setAttribute(Qt.WA_PaintOnScreen)
# self.setWindowFlag(Qt.FramelessWindowHint)
self.layout = QVBoxLayout()
self.layout.setAlignment(Qt.AlignRight)
self.button = QPushButton("X")
self.button.setFixedSize(QSize(40, 40))
self.layout.addWidget(self.button)
self.setLayout(self.layout)
self.button.clicked.connect(self.close)
self.show()
self.setFocus()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = MyWidget()
sys.exit(app.exec_())
Some comments on the lines I used.
self.setWindowOpacity(.8)
This line works, but it makes the titlebar transparent with the same amount. If I want to have full transparency, the titlebar would be invisible as well.
self.setStyleSheet("background:red;")
Some sources stated that this line would work, but it doesn’t seem to. It just makes it black. I put it to red here, so the button is visible at least.
self.setAttribute(Qt.WA_NoSystemBackground)
self.setAttribute(Qt.WA_PaintOnScreen)
These lines don’t seem to do anything
self.setAttribute(Qt.WA_TranslucentBackground)
self.setWindowFlag(Qt.FramelessWindowHint)
These lines work if used together, but they make everything disappear. Including the titlebar and the frame.
Any thoughts?