Tkinter tkFileDialog.askdirectory - returning directory above the one I want

时间:2015-06-15 14:52:19

标签: python tkinter

In Python, using Tkinter, I'm trying to use tkFileDialog.askdirectory to get the users selection of a directory. It works - in so much as the dialog screen shows up OK, and there is a list of directory names to choose from. I can then double click on a directory name and go down several levels. When I get to the directory name I want - I click on it once, then click the OK button.

Now, it returns the name of the directory I was in, not the one I selected! That is, for a directory tree like this:

/home/user/log/messages/file_1

I can go down through /home then user then log and single click on the directory called messages, then click OK button.

The returned string is "/home/user/log"

But, what I want is "/home/user/log/messages".

Why does it do that? it's not what I would expect.

import Tkinter, tkFileDialog

root = Tkinter.Tk()
dirname = tkFileDialog.askdirectory(parent=root, initialdir="/", title='Please select a directory')
if len(dirname ) > 0:
    print "You chose %s" % dirname

I'm using Python 2.7.6 on Ubuntu 14.04.1 LTS

1 个答案:

答案 0 :(得分:0)

你的问题的答案“它为什么这样做?”当然是:“因为它是按照这种方式编程的。” ; - )

事实上,你已经双重点击了你想要的目录。 返回值是你在对话底部看到的(“你的选择”或类似的东西 - 在德国它说:“Auswahl”)。

-yes -