Search code examples
pythonpython-2.7tkinterfiledialog

tkFileDialog AttributeError


I am having trouble using tkFileDialog.askopenfile() in Python 2.7.11

The following code produces the error.

import Tkinter

print Tkinter.TkVersion
Tkinter.tkFileDialog.askopenfile(filetypes=[('csvfiles','.csv')])

The Python Shell output is:

8.5

Traceback (most recent call last):
  File "C:/Users/User1/Desktop/tmp.py", line 4, in <module>
    Tkinter.tkFileDialog.askopenfile(filetypes=[('csvfiles','.csv')])
AttributeError: 'module' object has no attribute 'tkFileDialog'
>>> 

If I'm making a terribly stupid mistake then I apologize but I can't seem to find it. Otherwise, are there other dialog boxes I can use in Tkinter to have a user select a file? Thanks.


Solution

  • for python 2.7 its a separate module:

    from tkFileDialog import askopenfilename
    

    In python 3 its included in tkinter :

    from tkinter import filedialog as fd