954,541 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

ImportError: No module named tkFileDialog

Hi

With every piece of code i download to check out, i often find with python 3.1/Windows Vista that the code will start with

from Tkinter import *

but i need to change it to
from tkinter import *

as it seems to be case sensative, i tried renameing the directory to be Tkinter but it didn't work, so i left it as it was and just changed the code each time .. annoying but oh well.

In my next program i wanted an open dialogue which needed the "from tkFileDialog import *" but i get a module not found, renaming it to lower case like i do with others does not change anything. I thought maybe was one to download but google shows me example code, docs etc but no download leading me to believe i already have it but it can't be fouind due to the same kinda issue .. CAUSE i found a python script called "file dialogue.py" (and another file) IN the lib/tkinter folder.

Anyone with windows knows any workarounds for this issue .... this module naming and case sensative stuff is getting annoying.

Aiban
Light Poster
42 posts since Jul 2009
Reputation Points: 10
Solved Threads: 0
 

Try this.

>>> import tkinter as tk
>>> print(tk.filedialog.__doc__)
File selection dialog classes.
Classes:

- FileDialog
- LoadFileDialog
- SaveFileDialog

This module also presents tk common file dialogues, it provides interfaces
to the native file dialogues available in Tk 4.2 and newer, and the
directory dialogue available in Tk 8.3 and newer.
These interfaces were written by Fredrik Lundh, May 1997.

# Give you a list of method under tk
>>>dir(tk)
snippsat
Practically a Posting Shark
808 posts since Aug 2008
Reputation Points: 353
Solved Threads: 294
 

Yeah that worked, i typed that commands in IDLE and everything happened the same, so that means i do have the filedialogue module?

What now? how do i get it open in my code so i can use it?

Aiban
Light Poster
42 posts since Jul 2009
Reputation Points: 10
Solved Threads: 0
 

if it worked, then I would believe you can import that module like any other in python. I do not use Tk but I use python.. That would be my limited knowledge..

Clueless86
Junior Poster in Training
76 posts since Jul 2009
Reputation Points: 10
Solved Threads: 1
 

You can try this.
Test script from Ene Uran.

# use Tkinter's file dialog window to get
# a file name with full path, you can also use
# this to get filenames for a console program
# askopenfilename() gives one selected filename

# with Python25 use ...
#import Tkinter as tk
#from tkFileDialog import askopenfilename

# with Python30 use ...
import tkinter as tk
from tkinter.filedialog import askopenfilename

root = tk.Tk()
# show askopenfilename dialog without the Tkinter window
root.withdraw()

# default is all file types
file_name = askopenfilename()

print(file_name)
snippsat
Practically a Posting Shark
808 posts since Aug 2008
Reputation Points: 353
Solved Threads: 294
 

With the advent of Python3 Tkinter has turned into a package called tkinter. I usually add a try/except to pick the right import:

# a simple Tkinter number guessing game
# shows how to create a window, an entry, a label, a button,
# a button mouse click response, and how to use a grid for layout

import random
try:
    # for Python2
    import Tkinter as tk
except ImportError:
    # for Python3
    import tkinter as tk

def click():
    """the mouse click command response"""
    global rn
    # get the number from the entry area
    num = int(enter1.get())
    # check it out
    if num > rn:
        label2['text'] = str(num) + " guessed too high!"
    elif num < rn:
        label2['text'] = str(num) + " guessed too low!"
    else:
        s1 = str(num) + " guessed correctly!"
        s2 = "\n Let's start a new game!"
        label2['text'] = s1 + s2
        # pick a new random number
        rn = random.randrange(1, 11)
    enter1.delete(0, 2)


# create the window and give it a title
root = tk.Tk()
root.title("Heidi's Guess A Number Game")

# pick a random integer from 1 to 10
rn = random.randrange(1, 11)

# let the user know what is going on
label1 = tk.Label(root, text="Guess a number between 1 and 10 -->")
# layout this label in the specified row and column of the grid
# also pad with spaces along the x and y direction
label1.grid(row=1, column=1, padx=10, pady=10)

# this your input area
enter1 = tk.Entry(root, width=5, bg='yellow')
enter1.grid(row=1, column=2, padx=10)
# put the cursor into the entry field
enter1.focus()

# action button, right-click it to execute command
button1 = tk.Button(root, text=" Press to check the guess ",
    command=click)
button1.grid(row=2, column=1, columnspan=2, padx=10, pady=10)

# the result displays here
label2 = tk.Label(root, text="", width=50)
label2.grid(row=3, column=1, columnspan=2, pady=10)

# start the mouse/keyboard event loop
root.mainloop()


Note: do not change the package directory names!!!!

sneekula
Nearly a Posting Maven
2,427 posts since Oct 2006
Reputation Points: 961
Solved Threads: 212
 

I'm using Python3.1.2 and i can't do what snippsat says:

>>> import tkinter as tk
>>> print(tk.filedialog.__doc__)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'filedialog'
>>>

i can import tk.filedialog, but the fact that the code i cited works for Python3 and doesn't work fot Python3.1.2 is a little scary. Look what hapen in Python3.1.2:

>>> from tkinter import *
>>> filedialog
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'filedialog' is not defined
>>> from tkinter import filedialog
>>> filedialog
<module 'tkinter.filedialog' from '/usr/lib/python3.1/tkinter/filedialog.py'>
>>>
jgomo3
Newbie Poster
1 post since Aug 2010
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You