2016-12-07 156 views
0

我有這個簡單的程序。它有一個按鈕,當你按下它時,第二個窗口會出現。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() 

但有時如果關閉Main_window關閉它崩潰second_window,我得到一個消息,說「Python.exe已停止工作」之後。 任何人都可以幫忙嗎?

回答

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

第二種解決:上closeEvent(self, event)添加self.sec_window.deleteLater()能夠刪除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()