为什么 tkinter.filedialog.askdirectory 没有返回完整路径(使用选定的目录名)? [英] Why tkinter.filedialog.askdirectory isn't returning the full path (with the selected dirname)?

查看:85
本文介绍了为什么 tkinter.filedialog.askdirectory 没有返回完整路径(使用选定的目录名)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取通过 tkinter.filedialog.askdirectory 选择的目录的完整路径,但它只返回根文件夹的路径,例如,选择文件夹/root 将只返回/",这好像很奇怪,python3.5中如何获取返回完整路径?

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

root = tk.Tk()root.withdraw()dirname = filedialog.askdirectory(parent=root,initialdir="/",title='请选择一个目录')如果 len(dirname ) >0:print("您选择了 %s" % 目录名)

解决方案

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

简单来说:您需要双击要打开的文件夹,然后单击确定,不要简单地选择它,然后单击确定 在文件打开对话框窗口中.否则,您将打印您现在所在目录的路径 (initialdir = '/')

I'm trying to get the full path of a directory selected via tkinter.filedialog.askdirectory, but it only returns the path of the root folder, for example, selection the folder /root will return only '/', this seems strange, how to get it return the full path in python3.5?

import tkinter as tk from tkinter import filedialog

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)

解决方案

The filedialog.askdirectory() method returns an instance of filedialog.Directory class that need a PyObject command:

The mere selection of a folder does not represent a command, thus you do not get printed the folder you want. By double clicking on the folder you want represents a satisfying and coherent PyObject command resulting in the result you expect.

In simple words: you need to double click on the folder you want to open, then click Ok and not simply select it and click on Ok on the file open dialog window. Otherwise, you will get printed the path of the directory where you are now (initialdir = '/')

这篇关于为什么 tkinter.filedialog.askdirectory 没有返回完整路径(使用选定的目录名)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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