运行后立即关闭窗口

2022-01-12 00:00:00 python pyqt5 qt-designer pyuic

问题描述

我的代码用一个按钮调用一个窗口.单击按钮时,调用另一个窗口.但是第二个窗口立即关闭

My code calls one window with a button. When the button is clicked, call another window. But the second window closes immediately

基本"和windows_two"是 pyuic5 从 .ui 文件中生成的 .py 库

"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()
    

怎么了?

谢谢


解决方案

只要变量是本地,它就会被收集垃圾".一旦函数返回;这意味着变量可能引用的所有内容也将(可能)被删除.

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.

在您的情况下发生的情况是,在正确创建窗口时,当 Call_Second_Window 返回时(就在 Form.show 之后),它将立即被删除(由于垃圾收集)()).

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.

不幸的是,您的代码有点不正统(尤其是从 PyQt 的角度来看),所以我正在重构".它是为了使它更标准化、更好地面向对象,而且更重要的是,更易于阅读.

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_())

注意:如您所见,我将 call_Second_Window 的名称更改为小写的c",这是因为大写名称只能用于类和常量,而函数名称应始终以小写字母开头.这又是为了可读性,这在编程中非常重要,也是 python 的核心原则之一.在官方 Python 代码样式指南.

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.

相关文章