47

I'm trying to show a open file dialog using Tkinter in Python. Every example I find seems very easy to use, but they all start with the line:

import tkFileDialog

This line throws an error for me, saying

No module named 'tkFileDialog'

It seems my Python doesn't have tkFileDialog. So I tried searching for it, but it seems that you don't "download" Tkinter, it just comes with Python. Why is my Tkinter missing tkFileDialog? Is there somewhere I can acquire it so that I can use it?

Another thing I thought is that maybe it has changed names since the examples I've read were written. Is there a different way to import tkFileDialog in Python 3?

I'm running Windows 7 64-bit, Python version

3.4.1 (v3.4.1:c0e311e010fc, May 18 2014, 10:45:13) [MSC v.1600 64 bit (AMD64)]

Any help would be greatly appreciated!

gfrung4
  • 1,658
  • 3
  • 14
  • 22
  • 1
    @StevenVascellaro _Definitely_ a duplicate. Your recent edits to that question made it much more broadly applicable. Before it didn't mention tkFileDialog, which would make it hard to find if someone were Googling my error. But now that it does, it looks like a great resource! – gfrung4 Mar 30 '18 at 17:18

1 Answers1

103

That code would have worked fine in Python 2.x, but it is no longer valid. In Python 3.x, tkFileDialog was renamed to filedialog and placed inside the Tkinter package. Nowadays, you import it like so:

import tkinter.filedialog
# or
from tkinter import filedialog
  • The package Tkinter has been renamed to tkinter in Python 3, as well as other modules related to it. http://stackoverflow.com/questions/673174/file-dialogs-of-tkinter-in-python-3 – JinSnow Jan 18 '17 at 15:54
  • To support Python 2 and Python 3, just do `import tkFileDialog as filedialog` for Python 2 and `from tkinter import filedialog` for Python 3. (chuck it in an `if`/`else` block) – A T Mar 14 '20 at 08:22