在Python3中选择文件
问题描述:
Python 3中的tkFileDialog模块在哪里?问题Choosing a file in Python with simple Dialog参考使用模块:在Python3中选择文件
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
蟒蛇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.
,但它没有给出暗示新的名称是什么,以及搜索tkFileDialog和askopenfilename在3.2.2文档都没有返回值(甚至不日的映射Ë旧名称到新的子模块名称)
试图明显没有做插孔:
from tkinter import askopenfilename, asksaveasfilename
ImportError: cannot import name askopenfilename
你怎么称呼askopenfilename()相当于在Python 3?
答
您正在寻找tkinter.filedialog
如in the docs。
from tkinter import filedialog
您可以通过Python解释器运行help(filedialog)
看看什么方法/类是filedialog
。我认为filedialog.LoadFileDialog
是你要找的。
答
你可以尝试这样的事情:
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()
'filedialog'不是通过'从Tkinter的进口*'可用。你必须这样做'从tkinter.filedialog导入askopenfilename'。 – Shule
我刚刚添加了root.withdraw()调用,以删除讨厌的窗口。 我的代码在Python 3.4中工作正常 – user1741137