Skip to content Skip to sidebar Skip to footer

Window Closes Immediatelly After Run

My code calls one window with a button. When the button is clicked, call another window. But the second window closes immediately 'basic' and 'windows_two' are .py libraries genere

Solution 1:

Whenever a variable is local it gets "garbage collected" as soon as the function returns; this means that everything the variable might reference to will also be (possibly) deleted too.

What is happening in your case is that while the windows is correctly created, it will be immediately deleted (due to the garbage collection) when the Call_Second_Window returns (just after Form.show()).

To avoid that there is only one solution: make the reference to the object persistent. There are various approaches to achieve that, depending on the situation.

Unfortunately your code is a bit unorthodox (especially from a PyQt perspective), so I'm "refactoring" it in order to make it more standardized, better object oriented and, also importantly, easily readable.

import basic, windows_two
from PyQt5 import QtWidgets

classMainWindow(QtWidgets.QMainWindow):
    def__init__(self):
        super().__init__()
        self.ui = basic.Ui_MainWindow()
        self.ui.setupUi(self)
        self.ui.printButton.clicked.connect(self.call_Second_Window) 

        self.secondWindow = Nonedefcall_Second_Window(self):
        ifnot self.secondWindow:
            self.secondWindow = SecondWindow()
        self.secondWindow.show()


classSecondWindow(QtWidgets.QWidget):
    def__init__(self):
        super().__init__()
        self.ui = windows_two.Ui_Form()
        self.ui.setupUi(self)


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    
    mainWindow = MainWindow()
    mainWindow.show()

    sys.exit(app.exec_())

Note: As you can see, I changed the name of call_Second_Window with a lower "c", and that's because capitalized names should only be used for classes and constants, while function names should always start with a lower case. This is again for readability, which is very important in programming and one of the core principles of python. Read more about this and other important topics on the official Style Guide for Python Code.

Post a Comment for "Window Closes Immediatelly After Run"