在Python3中选择一个文件

Dav*_*ave 14 python tkinter python-3.x

Python 3中的tkFileDialog模块在哪里?使用简单的Dialog在Python中选择文件的问题引用模块使用:

from Tkinter import Tk
from tkFileDialog import askopenfilename
Run Code Online (Sandbox Code Playgroud)

但在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
Run Code Online (Sandbox Code Playgroud)

python 2.7.2 doc(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.
Run Code Online (Sandbox Code Playgroud)

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

尝试显而易见的不做杰克:

from tkinter import askopenfilename, asksaveasfilename
ImportError: cannot import name askopenfilename
Run Code Online (Sandbox Code Playgroud)

你如何在Python 3中调用相同的askopenfilename()?

brc*_*brc 34

您正在寻找文档中tkinter.filedialog提到的内容.

from tkinter import filedialog
Run Code Online (Sandbox Code Playgroud)

您可以filedialog通过help(filedialog)在python解释器中运行来查看所处的方法/类.我认为filedialog.LoadFileDialog这就是你要找的东西.


use*_*137 12

你可以尝试这样的事情:

from  tkinter import *
root = Tk()
root.filename =  filedialog.askopenfilename(initialdir = "E:/Images",title = "choose your file",filetypes = (("jpeg files","*.jpg"),("all files","*.*")))
print (root.filename)
root.withdraw()
Run Code Online (Sandbox Code Playgroud)

  • 无法从tkinter import *使用`filedialog`。您必须执行诸如“从tkinter.filedialog导入askopenfilename”之类的操作。 (2认同)