使用按钮事件控制'setAttribute'

时间:2018-11-18 01:37:34

标签: python pyqt pyqt5

我正在尝试使用位于here的示例来控制test。单击按钮时,我想使除按钮之外的背景透明。但它不起作用。我是菜鸟,所以我不知道这是怎么回事。

setAttribute

2 个答案:

答案 0 :(得分:0)

尝试一下:

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)

        layout = QtWidgets.QHBoxLayout(self)
        self.btn = QtWidgets.QPushButton("Make the background transparent ?")
        self.btn.setStyleSheet("""QPushButton { color : #000; font-size: 50px;}""")
        layout.addWidget(self.btn)

        self.btn.clicked.connect(self.Btn_clicked)

    def Btn_clicked(self) :
        self.setAttribute(Qt.WA_TranslucentBackground, True)
        self.setAttribute(Qt.WA_NoSystemBackground, False)

        if self.sender().text() == "Make the background transparent ?":
            self.btn.setText("Return the background back.")
            self.btn.setStyleSheet("""QPushButton { color:#fff; background-color:#000;font-size:50px;}""")
            self.setWindowOpacity(0.5)
        else:
            self.btn.setText("Make the background transparent ?")
            self.btn.setStyleSheet("""QPushButton {color: #000; font-size: 50px;}""")
            self.setWindowOpacity(1.0)

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

enter image description here

答案 1 :(得分:0)

我找到了this的方法!但是与此不同的是,我不知道它为什么能正常工作。

import sys
from PyQt5.QtCore import Qt
from PyQt5 import QtWidgets, QtCore, QtGui
class MainFrame(QtWidgets.QWidget):

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

        self.setWindowFlags(Qt.FramelessWindowHint)
        self.setFixedSize(400, 400)

        self.layout = QtWidgets.QHBoxLayout(self)
        self.btn = QtWidgets.QPushButton("TEST")
        self.layout.addWidget(self.btn)
        self.btn.clicked.connect(self.btn_clicked)

        self.setAttribute(Qt.WA_TranslucentBackground, True)
        self.setWindowFlags(Qt.FramelessWindowHint)
        self.istransparent = True

    def set_transparency(self) :
        self.btn_bl = not self.btn_bl
        self.set_transparency(self.btn_bl)

    def btn_clicked(self) :

        if not self.istransparent :
            print("transparent activated")
            self.istransparent = not self.istransparent
            self.setAttribute(Qt.WA_TranslucentBackground, self.istransparent)
        else:
            self.istransparent = not self.istransparent
            print("transparent deactivated")
            self.setAttribute(Qt.WA_NoSystemBackground, False)
            self.setAttribute(Qt.WA_TranslucentBackground, self.istransparent)
        self.repaint()

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