2011-11-03 67 views
11

Mô-đun tkFileDialog trong Python 3 ở đâu? Câu hỏi đặt ra tài liệu tham khảo Choosing a file in Python with simple Dialog module sử dụng:Chọn tệp trong Python3

from Tkinter import Tk 
from tkFileDialog import askopenfilename 

nhưng sử dụng đó (sau khi thay đổi Tkinter để Tkinter) trong Python 3 được:

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 

Các python 2.7.2 doc (docs.python.org) nói:

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. 

nhưng nó mang lại không có gợi ý gì những cái tên mới sẽ là, và tìm kiếm tkFileDialog và askopenfilename trong 3.2.2 docs trả gì cả (thậm chí không phải là một ánh xạ từ thứ e tên cũ sang tên submodule mới)

Cố gắng rõ ràng không làm jack:.

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

Làm thế nào để bạn gọi tương đương với askopenfilename() trong Python 3?

Trả lời

28

Bạn đang tìm kiếm tkinter.filedialog như đã ghi chú in the docs.

from tkinter import filedialog 

Bạn có thể nhìn vào những gì các phương pháp/lớp học nằm trong filedialog bằng cách chạy help(filedialog) trong thông dịch viên python. Tôi nghĩ rằng filedialog.LoadFileDialog là những gì bạn đang tìm kiếm.

8

Bạn có thể thử một cái gì đó như thế này:

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' không có sẵn thông qua' từ Tkinter nhập khẩu * '. Bạn phải làm như 'từ tkinter.filedialog import askopenfilename'. – Shule

+1

Tôi vừa thêm lệnh gọi root.withdraw() để xóa cửa sổ pesky. Mã của tôi hoạt động tốt trong Python 3.4 – user1741137

Các vấn đề liên quan