【问题标题】:QtGui.QFileDialog.getExistingDirectory() window won't close after directory has been chosen (PyQt)选择目录后,QtGui.QFileDialog.getExistingDirectory() 窗口不会关闭(PyQt)
【发布时间】:2018-02-19 02:36:23
【问题描述】:

我正在尝试在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。 谢谢! 干杯

【问题讨论】:

  • 我无法使用您发布的代码重现您描述的问题(在 Linux 上测试)。
  • 我添加了一个带有一些代码的 main 语句,以便错误接缝更加明显。当程序停止从命令行输入时,应关闭窗口。
  • 一点也不明显。对我来说事件顺序: 1. 运行脚本。 2. 出现消息框(“文件夹路径尚未设置”)。 3. 文件对话框出现。 4.选择文件夹,点击确定。 5. 文件对话框关闭。 6.控制台打印选中的文件夹并输入指令。
  • 好的,那么我的系统有问题!因为当程序停止输入指令时,对话窗口仍然为我打开。我在两台不同的计算机上都有同样的问题,但都在 Ubuntu 12.04 上......
  • 那么你的意思是当你在对话框中点击“确定”时,它并没有关闭?还是对话框是无模式的,所以get_new_path函数在得到folder之前返回?

标签: python ubuntu pyqt qfiledialog


【解决方案1】:

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

该问题似乎是由于尝试退出 get_new_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()

【讨论】:

    猜你喜欢
    • 2013-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多