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

Question:

I want to close all other windows opened by the main window when the main window is closed.

Please find below the min. code that I was testing:

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

As shown above I tried using the aboutToQuit option of the QApplication, but it only gets called when the another window also is closed.

I want to close the another window automaticaly when the mainwindow is closed.

Asked By: tonyjosi

||

Answers:

Implement the closeEvent:

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

Note that you can also use QApplication.closeAllWindows() to close any top level window, even without having any direct reference, but if any of those windows ignores the closeEvent() the function will stop trying to close the remaining.

To avoid that, you can cycle all windows using QApplication.topLevelWidgets(); windows ignoring the closeEvent will still keep themselves open, but all the others will be closed:

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

You could try to use signals:

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

This mechanism allows to close another window even without closing the main window.

(I used signals for other purposes, hope this works as well)

Answered By: Nechoj

try this one

def closeEvent(self, event):
   QApplication.closeAllWindows()
   event.accept()

with QApplication.closeAllWindows() will close every open window

Answered By: Paris Thiva
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.