如何在 Tkinter 中使用相同的对话框来浏览和选择文件和目录? [英] How can I use the same dialog box in Tkinter to browse and select files and directories?

查看:36
本文介绍了如何在 Tkinter 中使用相同的对话框来浏览和选择文件和目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Tkinter 为 Python 脚本构建 GUI.我需要一个按钮来打开一个对话框,允许我选择文件和目录.直到现在,我遇到了

I am using Tkinter for building a GUI for a python script. I need a button which opens up a dialog box which allows me to select both files and directories. Till now, I've come across

tkFileDialog.askdirectory(parent=root, title=dirtext1)

它只允许在对话框中选择目录并且,

which allows just to select directories in the dialog box and,

tkFileDialog.askopenfilename(parent=root, title=filetext)

它允许我只选择文件.到目前为止,我使用单独的按钮访问这些对话框,每个按钮调用这些功能之一.无论如何,是否可以使用单个对话框选择文件或文件夹?

which allows me to just select files. As of now, I access these dialog boxes using separate buttons, each of which calls one of these functions. Is there anyway to select either a file or a folder using a single dialog box?

推荐答案

我不这么认为.没有内置类可以轻松完成

I don't think so. There is no built-in class to do it easily

调查

如果您查看 tkFileDialog 模块的源代码,OpenDirectory 类都继承自 _Dialog,位于tkCommonDialog.

If you look at the tkFileDialog module's source code, both the Open and the Directory classes inherit from _Dialog, located in tkCommonDialog.

到目前为止还好;这些类很简单,只扩展了两种方法._fixresult 是一个根据您的选项进行过滤的钩子(有前途的),并且 _fixoptions 添加正确的 tcl 参数(如初始目录).

Good so far; these classes are simple and only extend two methods. _fixresult is a hook that filters based on your options (promising), and _fixoptions adds the right tcl parameters (like initial directory).

但是当我们到达 Dialog 类(_Dialog 的父类)时,我们看到它通过给定的名称调用 tcl 命令.内置名称是tk_getOpenFile"和tk_chooseDirectory".在此之后,我们没有很多 Python 级别的命令自由.如果我们去看看其他 tcl 脚本可用,我们很失望.

But when we get to the Dialog class (parent of _Dialog), we see that it calls a tcl command by a given name. The names built-in are "tk_getOpenFile" and "tk_chooseDirectory". We don't have a lot of python-level freedom of the command after this. If we go to see what other tcl scripts are avaliable, we are disappointed.

看起来您的选择是

  • ttk::getOpenFile
  • ttk::getSaveFile
  • ttk::chooseDirectory
  • ttk::getAppendFile

结论

老鼠!幸运的是,使用列表框、输入字段、按钮(和其他 tk 内置函数)和 os 模块制作自己的对话框应该很容易.

Rats! Luckily, it should be quite easy for you to make your own dialog using listboxes, entry fields, button, (and other tk-builtins), and the os module.

简单的替代方案

从您的评论看来,一个可行的简单解决方法可能是使用

From your comments, it looks like a viable simple work-around might be to use

directory = os.path.dirname(os.path.realpath(tkFileDialog.askopenfilename()))

他们必须选择一个文件,但打开"按钮将返回一个路径",因为路径是从文件位置计算出来的

They'll have to select a file, but the "Open" button will "return a path", in the sense that the path is computed from the file location

但我真的很想要!

如果由于某种原因你真的想要这种行为但不想重新制作小部件,你可以直接调用 tcl 脚本.可以从 getOpenFile 复制代码并提供更宽松的参数以允许选择更通用的对象.这不是我的专长,似乎是一个非常糟糕的主意,但是这里 是您直接调用 tcl 的方式,这里 是您可以了解有关底层命令的更多信息.

If for some reason you really want this behaviour but don't want to remake the widget, you can call tcl scripts directly. It may be possible to copy the code from getOpenFile and provide more loose arguments that allow for selecting a more generic object. This isn't my speciality and seems like a really bad idea, but here is how you call tcl directly and here is where you can learn more about the underlying commands.

这篇关于如何在 Tkinter 中使用相同的对话框来浏览和选择文件和目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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