简体   繁体   English

PyQt:打开后为什么新窗口会立即关闭

[英]PyQt: Why does new window close immediately after opening it

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. 我已经被困了一段时间了。

The window is disappearing because it goes out of scope at the end of your __init__ function. 窗口正在消失,因为它超出了__init__函数末尾的范围。 Since there are no further references to it, the python garbage collector removes it. 由于没有进一步的引用,python垃圾收集器将其删除。

Usually PyQt objects keep references to their children so this is not a problem. 通常PyQt对象会保留对子代的引用,因此这不是问题。 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. 显而易见的候选人是MainWindow类。

You can make win a member of MainWindow by using self.win = QWidget() instead of win = QWidget() . 您可以使用self.win = QWidget()而不是win = QWidget()来使win成为MainWindow的成员。 The window will now stay open for the lifetime of MainWindow unless you close it. 除非你关闭它,否则窗口将在MainWindow的生命周期内保持打开状态。

You have other problems with your code, but this explains why the window disappears. 您的代码还有其他问题,但这解释了窗口消失的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM