2016-12-07 9 views
0

私はこの簡単なプログラムを持っています。そのボタンがあり、それを押すと2番目のウィンドウが表示されます。pyqt4 2番目のウィンドウが閉じた後にメインウィンドウがクラッシュする

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

最初のソリューションsecond_windowMain_window間に関係がないので、きれいにする必要はありませんので、self.sec_window = second_window()self.sec_window = second_window(self)を変更します。次のコードが示すように

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() 
関連する問題