5

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.

tonyjosi
  • 717
  • 1
  • 9
  • 17

4 Answers4

8

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()
musicamante
  • 41,230
  • 6
  • 33
  • 58
0

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)

Nechoj
  • 1,512
  • 1
  • 8
  • 18
0

try this one

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

with QApplication.closeAllWindows() will close every open window

Paris Thiva
  • 1
  • 1
  • 3
0

try my method to close all windows from main window.

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

from PyQt5.QtGui import QCloseEvent

import sys


class AnotherWindow(QWidget):

      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_all_windows(self):
          win_list = QApplication.allWindows()
          for w in win_list:
              w.close()

      def closeEvent(self, event: QCloseEvent) -> None:
          close = QMessageBox()
          close.setText("voulez vous vraiment quitter l'application ?")
          close.setStandardButtons(QMessageBox.Yes | QMessageBox.Cancel)
          close = close.exec()

          if close == QMessageBox.Yes:
             event.accept()
             self.close_all_windows()
          else:
             event.ignore()


 if __name__ == "__main__":

    app = QApplication(sys.argv)

    w = MainWindow()
    w.show()

    sys.exit(app.exec())
artodraw
  • 1
  • 1