Come chiudere le altre finestre quando la finestra principale viene chiuso in pyqt5

0

Domanda

Voglio chiudere tutte le altre finestre aperte nella finestra principale quando la finestra principale viene chiuso.

Si prega di trovare sotto il min. il codice che mi è stato di prova:

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

Come mostrato sopra, ho provato ad usare il aboutToQuit opzione di QApplicationma viene chiamata quando l'altra finestra è chiusa.

Voglio chiudere la finestra di un altro automaticamente quando la mainwindow è chiuso.

pyqt5 python
2021-11-23 13:23:26
2

Migliore risposta

4

Implementare il closeEvent:

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

Si noti che è anche possibile utilizzare QApplication.closeAllWindows() per chiudere qualsiasi finestra di primo livello, anche senza avere alcun riferimento diretto, ma se uno di quelli windows ignora il closeEvent() la funzione di interrompere il tentativo di chiudere le altre.

Per evitare questo, è possibile scorrere tutte le finestre utilizzando QApplication.topLevelWidgets(); windows ignorando il closeEvent sarà ancora tenere aperto, ma tutti gli altri saranno chiusi:

    def closeEvent(self, event):
        for window in QApplication.topLevelWidgets():
            window.close()
2021-11-23 19:09:45
0

Si potrebbe provare a utilizzare segnali:

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

Questo meccanismo permette di chiudere in un'altra finestra anche senza chiudere la finestra principale.

(I segnali utilizzati per altri scopi, spero che questo funziona così)

2021-11-23 13:27:42

In altre lingue

Questa pagina è in altre lingue

Русский
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................