【问题标题】:control 'setAttribute' using button event [duplicate]使用按钮事件控制“setAttribute”[重复]
【发布时间】:2018-11-19 03:30:25
【问题描述】:

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

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("TEST")
        layout.addWidget(self.btn)

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

    def Btn_clicked(self) :
        self.setAttribute(Qt.WA_TranslucentBackground)
        print("clicked")

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

【问题讨论】:

    标签: python pyqt pyqt5


    【解决方案1】:

    试试看:

    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_()
    

    【讨论】:

    • 有没有办法只让背景透明?这也使按钮透明。
    【解决方案2】:

    我找到了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_()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-28
      • 1970-01-01
      • 1970-01-01
      • 2017-05-09
      相关资源
      最近更新 更多