03
Май
2020

Как закрыть все окна при закрытии одного?

Подскажите как закрыть все активные окна при закрытии MainWindow ?

import sys
from PyQt5 import QtWidgets

class Window1(QtWidgets.QWidget):
    def __init__(self, parent=None):
        QtWidgets.QWidget.__init__(self, parent)

        label = QtWidgets.QLabel("Label 1")

        layout = QtWidgets.QVBoxLayout()
        layout.addWidget(label)
        self.setLayout(layout)

class Window2(QtWidgets.QWidget):
    def __init__(self, parent=None):
        QtWidgets.QWidget.__init__(self, parent)

        label = QtWidgets.QLabel("Label 2")

        layout = QtWidgets.QVBoxLayout()
        layout.addWidget(label)
        self.setLayout(layout)

class MainWindow(QtWidgets.QWidget):
    def __init__(self, parent=None):
        QtWidgets.QWidget.__init__(self, parent)

        window1_button = QtWidgets.QPushButton("Window 1")
        window2_button = QtWidgets.QPushButton("Window 2")
        window_quit = QtWidgets.QPushButton("Close")

        layout = QtWidgets.QVBoxLayout()
        layout.addWidget(window1_button)
        layout.addWidget(window2_button)
        layout.addWidget(window_quit)

        self.setLayout(layout)

        window1_button.clicked.connect(self.open_window1)
        window2_button.clicked.connect(self.open_window2)
        window_quit.clicked.connect(self.quit)

    def open_window1(self):
        window = Window1()
        window.resize(300, 300)
        window.show()

    def open_window2(self):
        window = Window2()
        window.resize(300, 300)
        window.show()

    def quit(self):
        QtWidgets.qApp.quit()

if __name__ == '__main__'
    application = QtWidgets.QApplication([])

    window = MainWindow()
    window.resize(500, 500)
    window.show()

    sys.exit(application.exec_())

Источник: https://ru.stackoverflow.com/questions/1119791/%D0%9A%D0%B0%D0%BA-%D0%B7%D0%B0%D0%BA%D1%80%D1%8B%D1%82%D1%8C-%D0%B2%D1%81%D0%B5-%D0%BE%D0%BA%D0%BD%D0%B0-%D0%BF%D1%80%D0%B8-%D0%B7%D0%B0%D0%BA%D1%80%D1%8B%D1%82%D0%B8%D0%B8-%D0%BE%D0%B4%D0%BD%D0%BE%D0%B3%D0%BE

Тебе может это понравится...

Добавить комментарий