2011-11-03 45 views
11

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?

回答

28

您正在尋找tkinter.filedialogin the docs

from tkinter import filedialog 

您可以通過Python解釋器運行help(filedialog)看看什麼方法/類是filedialog。我認爲filedialog.LoadFileDialog是你要找的。

8

你可以嘗試這樣的事情:

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() 
+1

'filedialog'不是通過'從Tkinter的進口*'可用。你必須這樣做'從tkinter.filedialog導入askopenfilename'。 – Shule

+1

我剛剛添加了root.withdraw()調用,以刪除討厭的窗口。 我的代碼在Python 3.4中工作正常 – user1741137