im doing a tut for Tkinter and in it they do this:

tkFileDialog.askopenfilename()

but when i run it i get this error:
Traceback (most recent call last):
File "C:\Documents and Settings\tom\Desktop\Programing\Python\c++ + python\main.py", line 67, in <module>
if __name__ == "__main__": main()
File "C:\Documents and Settings\tom\Desktop\Programing\Python\c++ + python\main.py", line 64, in main
app = App(root)
File "C:\Documents and Settings\tom\Desktop\Programing\Python\c++ + python\main.py", line 13, in __init__
filemenu.add_command(label = "Open", command = self.openFile())
File "C:\Documents and Settings\tom\Desktop\Programing\Python\c++ + python\main.py", line 42, in openFile
tkFileDialog.askopenfilename()
NameError: global name 'tkFileDialog' is not defined

any ideas?

Recommended Answers

All 5 Replies

Can I see the rest of your code?
I don't use Tk (I find wxPython to be much better), but what happens if you replace that line with

tk.FileDialog.askopenfilename()

Of course, that would assume tk has a class named FileDialog and that you use Tkinter like

import tk

Hope that helped with something.

Can I see the rest of your code?
I don't use Tk (I find wxPython to be much better), but what happens if you replace that line with

tk.FileDialog.askopenfilename()

Of course, that would assume tk has a class named FileDialog and that you use Tkinter like

import tk

Hope that helped with something.

Yeah Shadwickman is right. Anything with TK you have to import it first before it will work. Good Luck

Yeah Shadwickman is right. Anything with TK you have to import it first before it will work. Good Luck

Or any other modules (or parts of modules) you want to use, like os, sys, time, threading, wx, etc. :P

Here would be a typical example using the tkFileDialog ...

# using the file dialog without a tk window

import Tkinter as tk
import tkFileDialog

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

# defaults to current directory and all file types
filename = tkFileDialog.askopenfilename()

print(filename)

thanks vageseat and otheres

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.