简体   繁体   中英

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) : 在此处输入图片说明 here is what I need (NO transparent button and transparent QWidget) : 在此处输入图片说明 Thank you very much.

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:

透明的QWidget

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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