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 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.
但它没有提示新名称是什么,并且在3.2.2文档中搜索tkFileDialog和askopenfilename根本不返回任何内容(甚至没有从旧名称到新子模块名称的映射。)
尝试显而易见的不做杰克:
from tkinter import askopenfilename, asksaveasfilename
ImportError: cannot import name askopenfilename
如何在Python 3中调用相同的askopenfilename()?
答案 0 :(得分:32)
您正在寻找tkinter.filedialog
,in the docs。
from tkinter import filedialog
您可以通过在python解释器中运行filedialog
来查看help(filedialog)
中的哪些方法/类。我认为filedialog.LoadFileDialog
正是您所寻找的。 p>
答案 1 :(得分:11)
您可以尝试这样的事情:
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()
答案 2 :(得分:1)
您需要先导入文件对话框,您可以按照以下步骤进行操作:
from tkinter import *
from tkinter import filedialog
root = Tk()
root.filename = filedialog.askopenfilename(initialdir = "/", title = "Select file")
print (root.filename)
root.mainloop()