Closing a window with close() will close all open windows

Question:

I am using Python and I have a main window with a button that opens another window which contain stackedWidget, when the user click "Finish" on the second window I want to close it. but when i try to use self.close() method it closing the main windows as well.

here is the part in my code that handle the Finish click event on the second window:

def click_next(self):

        if self.next.text()!="Finish>":
         if self.stackedWidget.currentIndex()!=2:

          self.stackedWidget.setCurrentIndex(self.stackedWidget.currentIndex()+1)
         else:
            self.next.setText("Finish>")
            self.stackedWidget.setCurrentIndex(self.stackedWidget.currentIndex() + 1)
        else:
            self.close()

Is there another way to close one window and keep the main open.

Thanks.


So for the example I created 2 windows ( main and screen1), the button in main will open screen1 and the button in screen1 expected to close only screen1 but it close both windows.
code for main window:

 from PyQt5 import QtCore, QtGui, QtWidgets
    from screen1 import Ui_screen1
    class Ui_MainWindow(object):
        def setupUi(self, MainWindow):
            MainWindow.setObjectName("MainWindow")
            MainWindow.resize(584, 567)
            self.centralwidget = QtWidgets.QWidget(MainWindow)
            self.centralwidget.setObjectName("centralwidget")
            self.button = QtWidgets.QPushButton(self.centralwidget, clicked=lambda:

 self.gotoscreen1())
        self.button.setGeometry(QtCore.QRect(220, 260, 131, 51))
        self.button.setObjectName("buton")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 584, 21))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)
        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)
    def gotoscreen1(self):
        self.window = QtWidgets.QMainWindow()
        self.ui = Ui_screen1()
        self.ui.setupUi(self.window)
        self.window.show()
    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.button.setText(_translate("MainWindow", "go to screen1"))
if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

code for screen1:

from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_screen1(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(384, 567)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.button = QtWidgets.QPushButton(self.centralwidget, clicked=lambda: self.close())

        self.button.setGeometry(QtCore.QRect(120, 260, 131, 51))
        self.button.setObjectName("buton")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 584, 21))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)
    def close(self):
        self.close()
    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.button.setText(_translate("MainWindow", "Close"))


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_screen1()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())
Asked By: Nir Eyal

||

Answers:

The problem isn’t that it is closing both windows, it’s that your whole app is crashing because it has reached the maximum recursion depth.

The issue is with your close method.

    def close(self):
        self.close()

All this is doing is calling itself over and over and over again until it crashes the program.

To fix this simply remove the close method completely, then in your setupUi method change the line that creates the self.button to:

self.button = QtWidgets.QPushButton(self.centralwidget, clicked=MainWindow.close)

I would also recommend subclassing QMainWindow instead of using the uic-like design.

Answered By: Alexander
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.