Python: How Close (or exit) Python Process After QMainWindow Dialog is closed
Question: There are two .py files placed in a same folder:

    
    
    dialog_01.py and dialog_02.py
    

Both files are copy of each other. Both build a simple QMainWindow dialog
window with two buttons: 'Ok' and 'Cancel'.

Clicking 'Ok' button closes a currently opened dialog window and opens
another. So if Dialog_01's Ok button is clicked the dialog_01 is closed and
dialog_02 is opened. If Dialog_02's Ok button is clicked then dialog_02 is
closed and dialog_01 is opened and so on.

# EDITED QUESTION:

Closing the dialog box leaves Python process still running at the background
(it can be seen in OSX Activity Monitor or Windows Task Manager).

**How to make sure the Python process is terminated after the dialog window is
closed?**

# dialog_01.py

    
    
    import sys, os
    from PyQt4 import QtCore, QtGui    
    if 'app' not in globals().keys(): app = QtGui.QApplication(sys.argv)
    
    class Dialog_01(QtGui.QMainWindow):
        def __init__(self):
            super(QtGui.QMainWindow,self).__init__()
    
            myQWidget = QtGui.QWidget()
            myBoxLayout = QtGui.QVBoxLayout()       
    
            Cancel_button = QtGui.QPushButton("Cancel")
            Cancel_button.clicked.connect(self.Cancel)      
            myBoxLayout.addWidget(Cancel_button) 
    
    
            Button_01 = QtGui.QPushButton("Press to close this dialog and open Dialog 02")
            Button_01.clicked.connect(self.callAnotherQMainWindow)
            myBoxLayout.addWidget(Button_01)        
    
            myQWidget.setLayout(myBoxLayout)
            self.setCentralWidget(myQWidget)
            self.setWindowTitle('Dialog 01')
    
        def callAnotherQMainWindow(self):
            from dialog_02 import Dialog_02
            self.close()
            self.dialog_02 = Dialog_02()
            self.dialog_02.show()
            self.dialog_02.raise_()
    
        def Cancel(self):
            self.close()
    
        def closeEvent(self):
            self.deleteLater()
    
    
    
    if __name__ == '__main__':
        dialog_1 = Dialog_01()
        dialog_1.show()
        dialog_1.resize(480,320)
        sys.exit(app.exec_())
    

# dialog_02.py

    
    
    import sys, os
    from PyQt4 import QtCore, QtGui    
    if 'app' not in globals().keys(): app = QtGui.QApplication(sys.argv)
    
    class Dialog_02(QtGui.QMainWindow):
        def __init__(self):
            super(QtGui.QMainWindow,self).__init__()
    
            myQWidget = QtGui.QWidget()
            myBoxLayout = QtGui.QVBoxLayout()
    
            Cancel_button = QtGui.QPushButton("Cancel")
            Cancel_button.clicked.connect(self.Cancel)      
            myBoxLayout.addWidget(Cancel_button) 
    
    
            Button_02 = QtGui.QPushButton("Press to close this dialog 02 and re-open Dialog 01")
            Button_02.clicked.connect(self.callAnotherQMainWindow)
            myBoxLayout.addWidget(Button_02)
    
            myQWidget.setLayout(myBoxLayout)
            self.setCentralWidget(myQWidget)
            self.setWindowTitle('Dialog 02')
    
        def callAnotherQMainWindow(self):
            from dialog_01 import Dialog_01
            self.close()
            self.dialog_01 = Dialog_01()
            self.dialog_01.show()
            self.dialog_01.raise_()
    
        def Cancel(self):
            self.close()
    
        def closeEvent(self):
            self.deleteLater()
    
    
    if __name__ == '__main__':
        dialog_2 = Dialog_02()
        dialog_2.show()
        dialog_2.resize(480,320)
        sys.exit(app.exec_())
    


Answer: Override the closeEvent() function of your QtGui.QMainWindow classes like
below:

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

This seemed to work:

Removing this:

    
    
    if 'app' not in globals().keys(): app = QtGui.QApplication(sys.argv)
    

and this:

    
    
    if __name__ == '__main__':
        dialog_2 = Dialog_02()
        dialog_2.show()
        dialog_2.resize(480,320)
        sys.exit(app.exec_())
    

...from dialog2 file did the trick.

