python - 为什么 tkinter.filedialog.askdirectory 不返回完整路径(带有选定的目录名)?

标签 python tkinter python-3.5

我试图获取通过 tkinter.filedialog.askdirectory 选择的目录的完整路径,但它只返回根文件夹的路径,例如,选择文件夹/root 将仅返回“/”,这看起来很奇怪,如何让它返回python3.5中的完整路径?

将 tkinter 导入为 tk 从 tkinter 导入文件对话框

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

最佳答案

filedialog.askdirectory()方法返回 filedialog.Directory 的实例需要 PyObject 的类(class)命令:

enter image description here

仅仅选择一个文件夹并不代表一个命令,因此您不会打印出您想要的文件夹。通过双击您想要的文件夹代表一个令人满意且连贯的 PyObject 命令,从而产生您期望的结果。

简单来说:您需要双击要打开的文件夹,然后单击“确定”,不是简单地选择它,然后单击确定 在文件打开对话框窗口上。否则,您将得到当前所在目录的路径 (initialdir = '/')

关于python - 为什么 tkinter.filedialog.askdirectory 不返回完整路径(带有选定的目录名)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36105688/

相关文章:

Python 3.5 与 3.6 相比,是什么让 "map"比理解更慢

Python 子进程在 stderr 中显示输出

python - 在 Python 中忽略来自第三方包的日志消息

python - 属性错误: 'function' object has no attribute <Variable>

python - 如何更改seaborn tsplot 的大小?

Python tkinter ttk 一个单选按钮出现大小错误

python - 如何自动扩展 QTreeWidget 项

Python Tkinter 字体选择器

python - 在实际打印文档之前预览 PDF 文件并选择打印机

Python:递归isinstance检查