Window closes immediatelly after run

爱⌒轻易说出口 提交于 2021-02-04 21:55:07

问题


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 genereted by pyuic5 from .ui files

import basic, windows_two
from PyQt5 import QtCore, QtGui, QtWidgets

if __name__ == "__main__":
    
    #Declarations
    import sys
    app = QtWidgets.QApplication(sys.argv)
    
    def Call_Second_Window():
        #Second Screen
        Form = QtWidgets.QWidget()
        ui = windows_two.Ui_Form()
        ui.setupUi(Form)
        Form.show()
        
    def Call_Main_Window():
        #MainWindow
        MainWindow = QtWidgets.QMainWindow()
        ui = basic.Ui_MainWindow()
        ui.setupUi(MainWindow)      
        ui.printButton.clicked.connect(Call_Second_Window) #click event to second window
        MainWindow.show()
        sys.exit(app.exec_())
        
    
    Call_Main_Window()
    

Whats wrong?

Thanks


回答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

class MainWindow(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 = None

    def call_Second_Window(self):
        if not self.secondWindow:
            self.secondWindow = SecondWindow()
        self.secondWindow.show()


class SecondWindow(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.



来源:https://stackoverflow.com/questions/64796438/window-closes-immediatelly-after-run

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!