You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PyQt子窗口WindowTransparentForInput状态切换异常求助

问题分析与解决方法

你的代码存在两个关键问题,导致子窗口切换状态时直接关闭:

  • 错误调用isChecked:self.switch.isChecked是方法而非属性,必须加括号调用self.switch.isChecked(),否则条件判断永远为真,逻辑完全错误。
  • 设置窗口标志后未重新显示窗口:调用setWindowFlags会自动隐藏窗口,必须在修改标志后调用self.subwindow.show(),才能让子窗口保持显示并应用新的窗口属性。

修正后的完整代码:

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)
        # 修改窗口标志后重新显示窗口
        self.subwindow.show()


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

内容的提问来源于stack exchange,提问作者Susumu Kikkawa

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.02 09:15:48