Python QtGui.QFileDialog.getExistingDirectory()窗口赢得';t选择目录后关闭(PyQt)

Python QtGui.QFileDialog.getExistingDirectory()窗口赢得';t选择目录后关闭(PyQt),python,ubuntu,pyqt,qfiledialog,Python,Ubuntu,Pyqt,Qfiledialog,我试图通过python程序中的QtGui.QFileDialog.getExistingDirectory()对话框窗口获取路径,以便在程序的其余部分处于控制台输出时为用户简化操作。 我有这段代码用于此目的: import sys, os from PyQt4 import QtGui def getpath(filename, noPathFileMsg='', wrongFolderMsg='', selectFold

我试图通过
python
程序中的
QtGui.QFileDialog.getExistingDirectory()
对话框窗口获取路径,以便在程序的其余部分处于控制台输出时为用户简化操作。 我有这段代码用于此目的:

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!')
如果调用getpath函数,则对话框窗口将保持打开状态,直到调用该函数的脚本结束,而不是在此指令之后关闭:

folder = QtGui.QFileDialog.getExistingDirectory(None, selectFolderMsg)
如果运行此代码,它将创建一个文件,该文件将使用对话框窗口保存的目录保存在运行脚本的文件夹中

我做错了什么

顺便说一下,我在Ubuntu 12.04上。 非常感谢。
干杯

在虚拟机中设置Ubuntu 12.04后,我可以确认对话框在单击“打开”后没有正确关闭

问题似乎是由于试图退出
get\u new\u path
函数中的
QApplication
引起的

相反,您应该创建一个全局
QApplication
对象,并仅在脚本完成时退出:

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

我无法使用您发布的代码(在Linux上测试)重现您描述的问题。我添加了一个包含一些代码的主语句,以便错误更加明显。当程序停止从命令行输入时,窗口应该关闭。这一点都不明显。对我来说,事件的顺序是:1。运行脚本。2.出现消息框(“尚未设置文件夹的路径”)。3.“文件”对话框出现。4.选择文件夹,单击“确定”。5.文件对话框关闭。6.控制台打印所选文件夹和输入指令。确定,则我的系统出现问题!因为当程序停止输入指令时,对话框窗口仍然为我打开。我对两台不同的计算机有相同的问题,但都在Ubuntu 12.04上…所以你的意思是当你点击对话框中的“确定”时,它不会关闭?或者是对话框是非模态的,因此
get\u new\u path
函数在获取
文件夹之前返回?