python - 在 Python3 中选择一个文件

标签 python python-3.x tkinter

Python 3 中的 tkFileDialog 模块在哪里?问题Choosing a file in Python with simple Dialog引用模块使用:

from Tkinter import Tk
from tkFileDialog import askopenfilename

但是在 Python 3 中使用它(在将 Tkinter 更改为 tkinter 之后)得到:

Traceback (most recent call last):
  File "C:\Documents and Settings\me\My Documents\file.pyw", line 5, in <module>
    import tkFileDialog
ImportError: No module named tkFileDialog

python 2.7.2 文档 (docs.python.org) 说:

tkFileDialog
Common dialogs to allow the user to specify a file to open or save.

These have been renamed as well in Python 3.0; they were all made submodules of the new tkinter package.

但它没有给出新名称的提示,并且在 3.2.2 文档中搜索 tkFileDialog 和 askopenfilename 根本没有返回任何内容(甚至没有从旧名称到新子模块名称的映射。)

尝试显而易见的事情是行不通的:

from tkinter import askopenfilename, asksaveasfilename
ImportError: cannot import name askopenfilename

如何在 Python 3 中调用 askopenfilename() 的等价物?

最佳答案

您正在寻找 tkinter.filedialog,如 in the docs 所述.

from tkinter import filedialog

您可以通过在 python 解释器中运行 help(filedialog) 来查看 filedialog 中有哪些方法/类。我认为 filedialog.LoadFileDialog 是您要查找的内容。

关于python - 在 Python3 中选择一个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7994461/

相关文章:

python - 如何使用 BeautifulSoup 匹配仅包含指定类而不包含任何其他类的标签?

python - pandas 中高效的列内操作

python - 用于调用 TKinter GUI 的单元测试

python - 如何使用 Pycharm 安装 tkinter?

python - pyspark 中的主 URL 是什么?

javascript - Django Python 中未加载静态文件

python - 对于开源项目来说“太小”有多小?

python - Pandas - 根据日期将数据框拆分为多个数据框?

python - 将两个 dfs 与一列分组并连接所有行值

python - Pyautogui 在导入时更改窗口大小