Search code examples
pyqt5qmainwindow

PyQt5) When I try to open Second window, it is closed without any error messages


I'm trying to open second window on main window.

I made 2 classes for each windows, and run the code. when I click the button on main window, the main window is closed and second windows show. but after 1 seconds the second window is closed and the process ends.

Really I don't know why it does...

from PyQt5 import QtWidgets
import sys

class MainWindow(QtWidgets.QMainWindow):
    def __init__(self):
        super(MainWindow, self).__init__()
        self.resize(400, 300)

        # Button
        self.button = QtWidgets.QPushButton(self)
        self.button.setGeometry(0, 0, 400, 300)
        self.button.setText('Open Sub Window')
        self.button.setStyleSheet('font-size:40px')
        self.button.clicked.connect(self.sub_show)

    def sub_show(self):
        self.hide()
        self.sub_window = SubWindow()
        self.sub_window.exec()
        self.show()

class SubWindow(QtWidgets.QWidget):
    def __init__(self):
        super(SubWindow, self).__init__()
        self.resize(400, 300)

        self.button2 = QtWidgets.QPushButton(self)
        self.button2.setGeometry(0, 0, 400, 300)
        self.button2.setText('Back to Main window')
        self.button2.setStyleSheet('font-size:40px')

        self.button2.clicked.connect(self.home)
        self.show()


    def home(self):
        self.close()

if __name__ == "__main__" :
    app = QtWidgets.QApplication([])
    mw = MainWindow()
    mw.show()
    sys.exit(app.exec())

Solution

  • You are calling show a few to many times. Also QWidget doesn't have an exec method. You will also want the first window to reappear when you close the second I assume.

    I made some changes in the example below. Give it a try:

    from PyQt5 import QtWidgets
    import sys
    
    class MainWindow(QtWidgets.QMainWindow):
        def __init__(self):
            super(MainWindow, self).__init__()
            self.resize(400, 300)
            self.button = QtWidgets.QPushButton(self)
            self.button.setGeometry(0, 0, 400, 300)
            self.button.setText('Open Sub Window')
            self.button.setStyleSheet('font-size:40px')
            self.button.clicked.connect(self.sub_show)
    
        def sub_show(self):
            self.hide()
            self.sub_window = SubWindow()
            # connect signal to show again when button2 is pressed
            self.sub_window.button2.clicked.connect(self.show)
            self.sub_window.show()
    
    class SubWindow(QtWidgets.QWidget):
        def __init__(self):
            super(SubWindow, self).__init__()
            self.resize(400, 300)
            self.button2 = QtWidgets.QPushButton(self)
            self.button2.setGeometry(0, 0, 400, 300)
            self.button2.setText('Back to Main window')
            self.button2.setStyleSheet('font-size:40px')
            self.button2.clicked.connect(self.close)
    
    if __name__ == "__main__" :
        app = QtWidgets.QApplication([])
        mw = MainWindow()
        mw.show()
        sys.exit(app.exec())