如何接近其他windows当主窗口关闭在pyqt5

0

的问题

我要关闭所有其他窗口打开的窗口的主要当主窗口关闭。

请查看以下分钟。 代码是我的测试:

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

如上所示,我试图使用 aboutToQuit 选项 QApplication但它只能被称为当另外一个窗口也被封闭。

我要关闭的另一个窗口automaticaly当mainwindow被关闭。

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

最好的答案

4

实施 closeEvent:

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

注意,你也可以使用 QApplication.closeAllWindows() 关闭 任何 顶级别窗口,即使不具有任何直接引用,但是,如果任何这些windows忽略了 closeEvent() 该职能将停止试图关闭剩余。

为了避免这种情况,你就可以循环使用所有的窗户 QApplication.topLevelWidgets();windows忽略了 closeEvent 仍然会保持自己打开,但 所有 其他人将被关闭:

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

你可以尝试使用的信号:

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

这种机制允许附近的另一个窗口,即使没有关闭主要的窗口。

(I使用的信号,用于其他目的,希望这个工作很好)

2021-11-23 13:27:42

其他语言

此页面有其他语言版本

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