pyqt4第二个窗口关闭后主窗口崩溃

时间:2016-12-07 21:12:21

标签: python-3.x pyqt4

我有这个简单的程序。它有一个按钮,按下它时会出现第二个窗口。

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

    import urllib.request

    class second_window(QDialog):
        def __init__(self, parent=None):
            QDialog.__init__(self)
            super(second_window, self).__init__(parent)

            layout = QVBoxLayout()

            button = QPushButton("close") 

            button.clicked.connect(self.button_clicked)

            layout.addWidget(button)
            self.setLayout(layout)
        def button_clicked(self):
            self.close()

    class Main_window(QDialog):

        def __init__(self):
            QDialog.__init__(self)
            layout = QVBoxLayout()

            button = QPushButton("Second Window")
            self.sec_window = second_window(self)

            layout.addWidget(button)

            button.clicked.connect(self.button_clicked)

            self.setLayout(layout)

        def button_clicked(self):
            self.sec_window.exec_()
    if __name__ == "__main__":
        app = QApplication(sys.argv)
        dl = window()
        dl.show()
        app.exec()

但有时如果你在关闭second_window之后关闭Main_window它会崩溃并且我收到一条消息“Python.exe已经停止工作”。 有人可以帮忙吗?

1 个答案:

答案 0 :(得分:1)

第一个解决方案:将self.sec_window = second_window(self)更改为self.sec_window = second_window(),因为没有必要进行清理,因为second_windowMain_window之间没有任何关系。如下面的代码所示

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


class second_window(QDialog):
    def __init__(self, parent=None):
        QDialog.__init__(self)
        super(second_window, self).__init__(parent)

        layout = QVBoxLayout()

        button = QPushButton("close")

        button.clicked.connect(self.button_clicked)

        layout.addWidget(button)
        self.setLayout(layout)

    def button_clicked(self):
        self.close()


class Main_window(QDialog):
    def __init__(self):
        QDialog.__init__(self)
        layout = QVBoxLayout()

        button = QPushButton("Second Window")
        self.sec_window = second_window()

        layout.addWidget(button)

        button.clicked.connect(self.button_clicked)

        self.setLayout(layout)

    def button_clicked(self):
        self.sec_window.exec_()



if __name__ == "__main__":
    app = QApplication(sys.argv)
    dl = Main_window()
    dl.show()
    app.exec()

第二个解决方案:在self.sec_window.deleteLater()上添加closeEvent(self, event),以便能够删除second_window

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


class second_window(QDialog):
    def __init__(self, parent=None):
        QDialog.__init__(self)
        super(second_window, self).__init__(parent)

        layout = QVBoxLayout()

        button = QPushButton("close")

        button.clicked.connect(self.button_clicked)

        layout.addWidget(button)
        self.setLayout(layout)

    def button_clicked(self):
        self.close()


class Main_window(QDialog):
    def __init__(self):
        QDialog.__init__(self)
        layout = QVBoxLayout()

        button = QPushButton("Second Window")
        self.sec_window = second_window(self)

        layout.addWidget(button)

        button.clicked.connect(self.button_clicked)

        self.setLayout(layout)

    def button_clicked(self):
        self.sec_window.exec_()

    def closeEvent(self, event):
        self.sec_window.deleteLater()
        super().closeEvent(event)



if __name__ == "__main__":
    app = QApplication(sys.argv)
    dl = Main_window()
    dl.show()
    app.exec()