PyQt:为什么新窗口打开后立即关闭 [英] PyQt: Why does new window close immediately after opening it

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

问题描述

我有一个主窗口,我想在按下按钮时打开另一个窗口(不是对话框).我的问题是新窗口在打开后几乎立即关闭.我已阅读可用的文章,并尝试实施解决方案,但似乎没有运气.这是我的全部代码:

I have a main window and I want to open a another window (not a dialog) on button press. My problem is that the new window closes almost immediately after it opens. I have read the available articles, and tried to implement the solutions, but seem to have no luck. This is my entire code:

import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *

class MainWindow (QMainWindow):
    def __init__(self):
        win = QWidget()
        win.adjustSize()
        grid=QGridLayout()
        grid.setRowStretch(0, 1)
        grid.setRowStretch(1, 1)
        grid.setRowStretch(5, 1)
        for i in range(0,5):
            for j in range(0,4):
                if i==0 and j==2:
                    l1=grid.addWidget(QLabel("Choose an option:"),i,j, 2, 2)
                if i==2 and j==1:
                    b1= QPushButton("Get Best Match")
                    grid.addWidget(b1,i,j)
                elif i==2 and j==2:
                    b2=QPushButton("Button2")
                    grid.addWidget(b2,i,j)
                elif i==2 and j==3:
                    b3=QPushButton("Button3")
                    grid.addWidget(b3,i,j)
        b5=grid.addWidget(QLabel(""),3,4) 
        b4=QPushButton("Button4")
        grid.addWidget(b4,2,4)
        w1=b1.clicked.connect(window1)
        b2.clicked.connect(Win2)
        b3.clicked.connect(Win3)
        b4.clicked.connect(Win4)            
        win.setLayout(grid)
        win.setGeometry(100,100,width//2,height//2,)
        win.setWindowTitle("PYQT")
        win.show()
        win.setStyleSheet("""
        .QPushButton {
        height: 30px ;
        width: 20px ; 
        }
        .QLabel {
        qproperty-alignment: AlignCenter;
        font-size:12pt
         }

         """)
        sys.exit(app.exec_())

class window1():
    def __init__(self, pressed):
        super(window1, self).__init__()
        win1 = QWidget()
        win1.adjustSize()
        win1.setGeometry(100,100,width//2,height//2,)
        win1.setWindowTitle("Get Best Match")
        win1.show()

if __name__ == '__main__':

    app = QApplication(sys.argv)
    screen_resolution = app.desktop().screenGeometry()
    width, height = screen_resolution.width(), screen_resolution.height()
    main=MainWindow()

有人可以帮我解决这个问题吗?我已经被卡住了一段时间.

Could someone please help me with this? I have been stuck for some time now.

推荐答案

窗口正在消失,因为它在 __init__ 函数结束时超出了范围.由于没有进一步引用它,python 垃圾收集器将其删除.

The window is disappearing because it goes out of scope at the end of your __init__ function. Since there are no further references to it, the python garbage collector removes it.

通常 PyQt 对象会保留对其子对象的引用,因此这不是问题.由于您希望小部件在单独的窗口中打开,因此无法为其分配父级,因此您需要在其他地方存储对它的引用.显而易见的候选者是 MainWindow 类.

Usually PyQt objects keep references to their children so this is not a problem. Since you want the widget to open in a separate window, you can't assign it a parent, so you need to store a reference to it somewhere else. The obvious candidate is the MainWindow class.

您可以使用 self.win = QWidget() 而不是 win = QWidget().该窗口现在将在 MainWindow 的生命周期内保持打开状态,除非您关闭它.

You can make win a member of MainWindow by using self.win = QWidget() instead of win = QWidget(). The window will now stay open for the lifetime of MainWindow unless you close it.

您的代码有其他问题,但这解释了窗口消失的原因.

You have other problems with your code, but this explains why the window disappears.

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

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