透明框架pyqt5内的透明小部件 [英] 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"))
在此示例代码中,小部件 QPushButton 将显示为透明,标签和其他小部件也是如此.如何仅将透明度应用于我的类 MainFrame.
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.
这是我所拥有的(透明按钮和透明 QWidget):这是我需要的(没有透明按钮和透明 QWidget):非常感谢.
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_()
,结果是这样的:
如果你只想有一些透明度,你可能需要像 这个例子.
If you want to have only some transparency you might need to rewrite the paintEvent
like in this example.
这篇关于透明框架pyqt5内的透明小部件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!