运行后窗口立即关闭 [英] Window closes immediatelly after run

查看:100
本文介绍了运行后窗口立即关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

这篇关于运行后窗口立即关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆