Specify File path in tkinter File dialog

江枫思渺然 提交于 2019-12-11 04:23:04

问题


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?

Here is the relevant code:

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

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

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

回答1:


What you want is:

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

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



来源:https://stackoverflow.com/questions/31122704/specify-file-path-in-tkinter-file-dialog

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!