在tkinter文件对话框中指定文件路径 [英] Specify File path in tkinter File dialog

查看:883
本文介绍了在tkinter文件对话框中指定文件路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文件对话框来打开文件,但是,我要打开的文件与我编写的程序位于不同的目录中.文件对话框将打开到我所在的目录.有没有办法指定文件对话框的打开位置?

I have a file dialog to open a file, however, the file that I want to open is in a different directory than the program I wrote. The file dialog opens to the directory where I am. Is there a way to specify where the filedialog opens?

以下是相关代码:

root = Tk()
root.fileName = tkFileDialog.askopenfilename()
f = open(root.fileName, 'r')

我尝试将所需的路径添加到"askopenfilename"调用中,但这没有用:

I tried adding the path that I want into the "askopenfilename" call, but that didn't work:

root.fileName = tkFileDialog.askopenfilename('/C:')

推荐答案

您想要的是:

root.fileName = tkFileDialog.askopenfilename(initialdir = "C:/<whatever>")

此参数将允许您指定打开窗口的目录.

This argument will allow you to specify the directory to which the window will open up.

这篇关于在tkinter文件对话框中指定文件路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆