【问题标题】:PyQt, How to switch transparent-setting for subwindowPyQt,如何切换子窗口的透明设置
【发布时间】:2023-02-02 18:39:01
【问题描述】:

我想要切换子窗口的 WindowTransparentForInput 语句。 在下面的代码中,我写道,子窗口将关闭而不是切换语句。

你能指出问题所在吗?

import sys
from PyQt5 import QtWidgets as qtw, QtGui as qtg, QtCore as qtc

class Main(qtw.QWidget):
    def __init__(self):
        super().__init__()
        self.mainWindow = qtw.QWidget(self)
        self.mainWindow.setGeometry(100,100,200,200)
        label = qtw.QLabel('Main window', self)
        self.switch = qtw.QCheckBox('Transparent for input on sub window', self)
        self.switch.setChecked(False)
        self.switch.stateChanged.connect(self.switchAction)

        mainLayout = qtw.QVBoxLayout()
        self.setLayout(mainLayout)
        mainLayout.addWidget(label)
        mainLayout.addWidget(self.switch)

        self.subwindow = qtw.QWidget()
        self.subwindow.setGeometry(150,100,200,200)
        sublabel = qtw.QLabel('Sub window', self.subwindow)

        self.show()
        self.subwindow.show()
    
   
    def switchAction(self):
        if self.switch.isChecked:
            self.subwindow.setWindowFlags(qtc.Qt.WindowTransparentForInput | qtc.Qt.FramelessWindowHint)
        else:
            self.subwindow.setWindowFlags(qtc.Qt.FramelessWindowHint)


if __name__ == '__main__':
    app = qtw.QApplication(sys.argv)
    mw = Main()
    sys.exit(app.exec())

【问题讨论】:

    标签: pyqt5


    【解决方案1】:

    通过如下修改“switchAction()”解决了这个问题。 我很抱歉你的符号。

    def switchAction(self):
        if self.switch.isChecked():
            self.subwindow.close()
            self.subwindow.setWindowFlags(self.subwindow.windowFlags() | qtc.Qt.WindowTransparentForInput)
            self.subwindow.show()
        else:
            self.subwindow.close()
            self.subwindow.setWindowFlags(self.subwindow.windowFlags() & ~qtc.Qt.WindowTransparentForInput)
            self.subwindow.show()
    

    【讨论】:

      猜你喜欢
      • 2023-03-10
      • 1970-01-01
      • 2013-02-21
      • 2010-10-21
      • 1970-01-01
      • 1970-01-01
      • 2019-07-15
      • 2018-04-23
      • 2019-04-24
      相关资源
      最近更新 更多