python的gui界面选择目录打开文件_QtGui.QFileDialog.getExistingDirectory()选择目录后窗口不会关闭(PyQt)...

在Python程序中使用QtGui.QFileDialog.getExistingDirectory()获取目录时,对话框窗口不会自动关闭。问题出现在Ubuntu 12.04上,尽管成功选择了目录并保存到文件,但窗口仍保持打开状态直到脚本结束。代码中尝试通过QApplication.exit()关闭应用,但似乎未生效。
摘要由CSDN通过智能技术生成

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

^{pr2}$

如果您运行此代码,它将创建一个文件,该文件将与对话框窗口一起保存的目录保存在运行脚本的文件夹中。在

我做错什么了?在

顺便说一下,我在Ubuntu 12.04上。

谢谢您!

干杯

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值