如何设置窗口和它的图形场景透明(使用滑块)和只留下QPushButton visib

2024-09-30 16:37:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我希望我的窗口和它的qgraphicscene是透明的,这是由QSlider控制的。你知道吗

我试着到处寻找答案,但主要是将背景色设置为0:

self.setStyleSheet("background-color:rgba(0, 0, 0, 0);")

或者像这样:

self.setWindowFlags(Qt.FramelessWindowHint) 
self.setAttribute(Qt.WA_TranslucentBackground)

它的工作,但我想使滑块,将控制透明度的水平,我不知道我如何才能做到上面的命令。你知道吗

谁能告诉我怎么做吗?谢谢

我将在下面附上我的测试代码(我希望能够控制所有内容的透明度,但不要触摸滑块和按钮):

from PySide2.QtGui import QBrush, QColor
from PySide2.QtCore import QSize, Qt
from PySide2.QtWidgets import QDialog, QVBoxLayout, QGraphicsView, QGraphicsScene, QFrame, QSizePolicy, QApplication, QSlider, QPushButton

class MainWindow(QDialog):
    def __init__(self):
        QDialog.__init__(self)
        self.mainLayout = QVBoxLayout()
        self.graphicsWidget = MainGraphicsWidget()
        self.window = 'transp_test'
        self.title = 'transparent UI'
        self.size = (1000, 650)
        self.create()


    def create(self, **kwargs):
        self.setWindowTitle(self.title)
        self.resize(QSize(*self.size))
        self.setLayout(self.mainLayout)
        self.mainLayout.addWidget(self.graphicsWidget)


class MainGraphicsWidget(QGraphicsView):
    def __init__(self, parent=None):
        super(MainGraphicsWidget, self).__init__(parent)
        self._scene = QGraphicsScene(backgroundBrush = Qt.gray)
        self.setScene(self._scene)

        self.transpSlider = QSlider()
        self.transpSlider.setRange(0,100)

        self.mainButton = QPushButton('I want it to be "Test" button')

        self._scene.addWidget(self.mainButton)
        self._scene.addWidget(self.transpSlider)
        self.transpSlider.move(300, 100)

        self.setTransformationAnchor(QGraphicsView.AnchorUnderMouse)
        self.setResizeAnchor(QGraphicsView.AnchorUnderMouse)
        self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
        self.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
        self.setBackgroundBrush(QBrush(QColor(30, 30, 30)))
        self.setFrameShape(QFrame.NoFrame)
        self.setSizePolicy(QSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding))

if __name__ == '__main__':
    import sys

    app = QApplication(sys.argv)
    window = MainWindow()
    window.setGeometry(600, 300, 600, 600)
    window.show()
    sys.exit(app.exec_())

Tags: fromimportselfinitdefscenewindowqt
2条回答

这里有一个方法。你知道吗

class MainWindow(QDialog):
    def __init__(self):
        QDialog.__init__(self)
        # these two lines are needed to get a transparent background in windows
        self.setWindowFlags(Qt.FramelessWindowHint)
        self.setAttribute(Qt.WA_TranslucentBackground)

        # rest of class definition unchanged


class MainGraphicsWidget(QGraphicsView):
    def __init__(self, parent=None):
        super(MainGraphicsWidget, self).__init__(parent)
        # set transparent background color for the widget itself
        self.setStyleSheet("background-color: #00000000")
        self._scene = QGraphicsScene()
        self.setScene(self._scene)

        # create of slider and connect to slot for changing opacity.
        self.transpSlider = QSlider()
        self.transpSlider.setStyleSheet('background-color: #00000000')
        self.transpSlider.setRange(0,255)
        self.transpSlider.valueChanged.connect(self.set_opacity)
        self.transpSlider.setValue(255)

        # rest of __init__ unchanged

    def set_opacity(self, value):
        brush = self.backgroundBrush()
        color = brush.color()
        color.setAlpha(value)
        brush.setColor(color)
        self.setBackgroundBrush(color)

请注意,我已将滑块的范围更改为0-255,以便更容易地将不透明度从完全不透明更改为完全透明。你知道吗

试试看:

#from PySide2.QtGui import QBrush, QColor
#from PySide2.QtCore import QSize, Qt
#from PySide2.QtWidgets import QDialog, QVBoxLayout, QGraphicsView, QGraphicsScene, QFrame, QSizePolicy, QApplication, QSlider, QPushButton

from PyQt5 import QtCore, QtWidgets, QtGui
from PyQt5.QtGui import QBrush, QColor
from PyQt5.QtCore import QSize, Qt
from PyQt5.QtWidgets import (QDialog, QVBoxLayout, QGraphicsView, QGraphicsScene, 
                             QFrame, QSizePolicy, QApplication, QSlider, QPushButton)


class MainGraphicsWidget(QGraphicsView):
    def __init__(self, parent=None):
        super(MainGraphicsWidget, self).__init__(parent)

        self._scene = QGraphicsScene() 
        self.setScene(self._scene)
        self.transpSlider = QtWidgets.QSlider(
            QtCore.Qt.Horizontal,
            minimum=10,
            maximum=100,
            value=100,
            valueChanged=self.onValueChanged,
        )
        self.mainButton = QPushButton('I want it to be "Test" button \n QUIT')
        self.mainButton.resize(150, 150)
        self.mainButton.clicked.connect(parent.close)

        self._scene.addWidget(self.mainButton)
        self._scene.addWidget(self.transpSlider)
        self.transpSlider.move(300, 100)

        self.setTransformationAnchor(QGraphicsView.AnchorUnderMouse)
        self.setResizeAnchor(QGraphicsView.AnchorUnderMouse)
        c = QColor(220, 30, 30)
        c.setAlphaF(1) 
        self.setBackgroundBrush(QBrush(c))
        self.setFrameShape(QFrame.NoFrame)
        self.setSizePolicy(QSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding))

    @QtCore.pyqtSlot(int)
    def onValueChanged(self, value):
        c = QColor(220, 30, 30)
        c.setAlphaF(value * 0.01) 
        self.setBackgroundBrush(QBrush(c))
        window.setWindowOpacity(value * 0.03)   
        self.setStyleSheet("MainGraphicsWidget {{background-color: rgba(0, 215, 55, {});}}".format(value))  


class MainWindow(QDialog):
    def __init__(self):
        super().__init__()
        self.setAttribute(Qt.WA_NoSystemBackground, False)      
        self.setStyleSheet("MainWindow {background-color: rgba(0, 215, 55, 70);}")   

        self.graphicsWidget = MainGraphicsWidget(self)  

        self.window = 'transp_test'
        self.title  = 'transparent UI'
        self.size   = (1000, 650)
        self.setWindowTitle(self.title)
        self.resize(QSize(*self.size))

        self.mainLayout = QVBoxLayout()
        self.setLayout(self.mainLayout)       
        self.mainLayout.addWidget(self.graphicsWidget)


if __name__ == '__main__':
    import sys
    app = QApplication(sys.argv)
    window = MainWindow()
    window.setGeometry(600, 100, 600, 600)
    window.show()
    sys.exit(app.exec_())

enter image description here

相关问题 更多 >