syedelec
syedelec

Reputation: 1300

transparent widgets inside transparent frame pyqt5

I have a problem solving an issue for my program. When I create a transparent Widget that holds some other widgets, they become transparent too and I don't understand why.

from PyQt5 import QtWidgets
from PyQt5.QtCore import Qt

class MainFrame(QtWidgets.QWidget):

    def __init__(self, parent=None):
        super(MainFrame, self).__init__(parent)

        self.setWindowFlags(Qt.FramelessWindowHint)
        self.setFixedSize(860, 560)

        # Set the opacity
        self.setWindowOpacity(1 - 50 / 100)

        layout = QtWidgets.QHBoxLayout(self)

        layout.addWidget(QtWidgets.QPushButton("TEST"))

In this sample code, the widget QPushButton will appear transparent, it's the same with labels, and other widgets. How do I apply transparency ONLY to my class MainFrame.

Edit :

here is what I have (transparent button and transparent QWidget) : enter image description here here is what I need (NO transparent button and transparent QWidget) : enter image description here Thank you very much.

Upvotes: 2

Views: 3681

Answers (1)

armatita
armatita

Reputation: 13465

I believe you are looking for this:

self.setAttribute(Qt.WA_TranslucentBackground)

The full code adapted from your example is this:

import sys
from PyQt5 import QtWidgets
from PyQt5.QtCore import Qt

class MainFrame(QtWidgets.QWidget):

    def __init__(self, parent=None):
        super(MainFrame, self).__init__(parent)

        self.setWindowFlags(Qt.FramelessWindowHint)
        self.setFixedSize(860, 560)

        # Set the opacity
        #self.setWindowOpacity(1 - 50 / 100)

        layout = QtWidgets.QHBoxLayout(self)

        layout.addWidget(QtWidgets.QPushButton("TEST"))

        self.setAttribute(Qt.WA_TranslucentBackground)

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    Frame = MainFrame(None)
    Frame.show()
    app.exec_()

, and the result is this:

transparent QWidget

If you want to have only some transparency you might need to rewrite the paintEvent like in this example.

Upvotes: 4

Related Questions