[英]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. 在此示例代码中,小部件QPushButton将显示为透明,与标签和其他小部件相同。 How do I apply transparency ONLY to my class MainFrame.
我如何仅将透明性应用于我的类MainFrame。
Edit : 编辑:
here is what I have (transparent button and transparent QWidget) : 这是我所拥有的(透明按钮和透明QWidget):
here is what I need (NO transparent button and transparent QWidget) :
这是我需要的(没有透明按钮和透明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: ,结果是这样的:
If you want to have only some transparency you might need to rewrite the paintEvent
like in this example . 如果只希望具有一定的透明度,则可能需要像此示例一样重写
paintEvent
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.