Quiero cerrar todas las demás ventanas abiertas por la ventana principal cuando la ventana principal está cerrada.
Encuentre a continuación el min. código que estaba probando:
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() Como se muestra arriba, intenté usar la opción aboutToQuit de QApplication , pero solo se llama cuando la otra ventana también está cerrada.
Quiero cerrar la otra ventana automáticamente cuando se cierra la ventana principal.
Implementar el closeEvent :
class MainWindow(QMainWindow): w = None # ... def closeEvent(self, event): if self.w: self.w.close() Tenga en cuenta que también puede usar QApplication.closeAllWindows() para cerrar cualquier ventana de nivel superior, incluso sin tener ninguna referencia directa, pero si alguna de esas ventanas ignora closeEvent() , la función dejará de intentar cerrar el resto.
Para evitar eso, puede alternar todas las ventanas usando QApplication.topLevelWidgets() ; las ventanas que ignoran el closeEvent se mantendrán abiertas, pero todas las demás se cerrarán:
def closeEvent(self, event): for window in QApplication.topLevelWidgets(): window.close()Podrías intentar usar señales:
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 windowsEste mecanismo permite cerrar otra ventana incluso sin cerrar la ventana principal.
(Utilicé señales para otros fines, espero que esto también funcione)