14

Where is the tkFileDialog module in Python 3? The question Choosing a file in Python with simple Dialog references the module using:

from Tkinter import Tk
from tkFileDialog import askopenfilename

but using that (after changing Tkinter to tkinter) in Python 3 gets:

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

The python 2.7.2 doc (docs.python.org) says:

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.

but it gives no hint what the new names would be, and searching for tkFileDialog and askopenfilename in the 3.2.2 docs returns nothing at all (not even a mapping from the old names to the new submodule names.)

Trying the obvious doesn't do jack:

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

How do you call the equivalent of askopenfilename() in Python 3?

Community
  • 1
  • 1
Dave
  • 3,834
  • 2
  • 29
  • 44

2 Answers2

35

You're looking for tkinter.filedialog as noted in the docs.

from tkinter import filedialog

You can look at what methods/classes are in filedialog by running help(filedialog) in the python interpreter. I think filedialog.LoadFileDialog is what you're looking for.

brc
  • 5,281
  • 2
  • 29
  • 30
14

You can try something like this:

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()
user1741137
  • 4,949
  • 2
  • 19
  • 28
  • 5
    `filedialog` isn't available via `from tkinter import *`. You have to do such as `from tkinter.filedialog import askopenfilename`. – Brōtsyorfuzthrāx Mar 25 '16 at 06:26
  • 1
    I just added the root.withdraw() call, to remove the pesky window. My code worked fine in Python 3.4 – user1741137 Mar 25 '16 at 14:02
  • Better to put `root.withdraw()` before the call to the dialog box, otherwise it's present while the dialog box is open. – RufusVS Nov 07 '19 at 16:34