如何在pyqt5中关闭主window时关闭其他windows

How to close other windows when the main window is closed in pyqt5

我想在主 window 关闭时关闭由主 window 打开的所有其他 windows。

请在最小值下方查找。我正在测试的代码:

from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton, QLabel, QVBoxLayout, QWidget

import sys


class AnotherWindow(QWidget):
    """
    This "window" is a QWidget. If it has no parent, it
    will appear as a free-floating window as we want.
    """
    def __init__(self):
        super().__init__()
        layout = QVBoxLayout()
        self.label = QLabel("Another Window")
        layout.addWidget(self.label)
        self.setLayout(layout)


class MainWindow(QMainWindow):

    def __init__(self):
        super().__init__()
        self.button = QPushButton("Push for Window")
        self.button.clicked.connect(self.show_new_window)
        self.setCentralWidget(self.button)

    def show_new_window(self, checked):
        self.w = AnotherWindow()
        self.w.show()

    def close_another_window(self):
        if self.w:
            self.w.close()


app = QApplication(sys.argv)

w = MainWindow()
app.aboutToQuit.connect(w.close_another_window)
w.show()
app.exec()

如上所示,我尝试使用 QApplicationaboutToQuit 选项,但只有在另一个 window 也关闭时才会调用它。

我想在主 window 关闭时自动关闭另一个 window。

实施 closeEvent:

class MainWindow(QMainWindow):
    w = None
    # ...
    def closeEvent(self, event):
        if self.w:
            self.w.close()

请注意,即使没有任何直接引用,您也可以使用 QApplication.closeAllWindows() 关闭 任何 顶层 window,但如果其中任何一个 windows 忽略 closeEvent() 函数将停止尝试关闭剩余的。

为避免这种情况,您可以使用 QApplication.topLevelWidgets() 循环所有 windows; windows 忽略 closeEvent 仍然会保持打开状态,但 所有 其他人将关闭:

    def closeEvent(self, event):
        for window in QApplication.topLevelWidgets():
            window.close()

您可以尝试使用信号:

from PyQt5.QtCore import pyqtSignal

class AnotherWindow(QWidget, close_signal):
    """
    This "window" is a QWidget. If it has no parent, it
    will appear as a free-floating window as we want.
    """
    def __init__(self):
        super().__init__()
        self.close_signal = close_signal
        self.close_signal.connect(self.close_me)  # connect handler to signal
        layout = QVBoxLayout()
        self.label = QLabel("Another Window")
        layout.addWidget(self.label)
        self.setLayout(layout)
    
    def close_me(self):
        # handler for signal    
        self.close()


class MainWindow(QMainWindow):
    close_signal = pyqtSignal()

    def __init__(self):
        super().__init__()
        self.button = QPushButton("Push for Window")
        self.button.clicked.connect(self.show_new_window)
        self.setCentralWidget(self.button)

    def show_new_window(self, checked):
        self.w = AnotherWindow(self.close_signal)
        self.w.show()

    def close_another_window(self):
        self.close_signal.emit()  # fire signal to close other windows

此机制允许关闭另一个 window,即使不关闭主 window。

(我将信号用于其他目的,希望它也能起作用)