QtGui.QFileDialog.getExistingDirectory() window won't close after directory has been chosen (PyQt)
Question: I am trying to get a path with the `QtGui.QFileDialog.getExistingDirectory()`
dialog window in a `python` program to ease things up for users while the rest
of the program is in console output. I have this piece of code for this
purpose:

    
    
    import sys, os
    from PyQt4 import QtGui
    
    def getpath(filename,
                noPathFileMsg='',
                wrongFolderMsg='',
                selectFolderMsg=''):
    
        try:
            f = open('./'+filename,'r')
        except IOError:
            folder = get_new_path(filename,
                                         noPathFileMsg, 
                                         selectFolderMsg)
        else:
            folder = f.readline()
            f.close()
            currentDir = os.getcwd()
            try:
                os.chdir(folder)
            except:
                folder = get_new_path(filename,
                                             wrongFolderMsg,
                                             selectFolderMsg)
            else:
                os.chdir(currentDir)
        finally:
            return folder
    
    def get_new_path(filename,
                     infoMsg,
                     selectFolderMsg):
    
        app = QtGui.QApplication(sys.argv)
        QtGui.QMessageBox.about(None, 'No folder', infoMsg)
        folder = QtGui.QFileDialog.getExistingDirectory(None, selectFolderMsg)
        app.exit()
        if os.name == 'posix':
            folder += '/'
        elif os.name == 'nt':
            folder += '\\'
        g = open('./'+filename,'w')
        g.write(folder)
        g.close()
        return folder
    if __name__ == '__main__':
        folderPath = getpath('pathtofolder.txt',
                             noPathFileMsg='The path to the folder has not been set',
                             wrongFolderMsg='The path folder saved cannot be reached',
                             selectFolderMsg='Please select a folder')
        print folderPath
        var = input('The program stopped at the input instruction, the dialog window should now be closed!')
    

If I call the getpath function the dialog window stays open until the script
that called the function ends instead of closing just after this instruction:

    
    
    folder = QtGui.QFileDialog.getExistingDirectory(None, selectFolderMsg)
    

If you run this code it will create a file that saves the directory saved with
the dialog window in the folder where the script is run.

What did I do wrong?

By the way, I am on Ubuntu 12.04. Thank you! Cheers


Answer: After setting up Ubuntu 12.04 in a VM, I can confirm that the dialog doesn't
close properly after clicking "Open".

The problem seems to be caused by attempting to quit the `QApplication` inside
the `get_new_path` function.

Instead, you should create a single, global `QApplication` object, and only
quit it when the script completes:

    
    
    def get_new_path(filename, infoMsg, selectFolderMsg):
    
        QtGui.QMessageBox.about(None, 'No folder', infoMsg)
        folder = QtGui.QFileDialog.getExistingDirectory(None, selectFolderMsg)
        ...
    
    if __name__ == '__main__':
    
        app = QtGui.QApplication(sys.argv)
    
        folderPath = getpath(...)
    
        app.exit()
    

