Reputation: 141
I have a working application using Python 2.7 and Tkinter that uses these constructs:
from Tkinter import *
import Tkinter
import tkFileDialog
class Window(Frame):
#...
# other functional code
#...
def ChangeCWD(self): #CWD is current working directory
root = Tkinter.Tk()
root.withdraw()
directory = tkFileDialog.askdirectory( ... )
root = Tk()
root.mainloop()
It has labels, buttons, canvas, multiple frames and file dialogue boxes and it all works nicely. I have begun updating the code to work on Python 3.5 and, so far all functions seem to work except for the file dialog. This is where I have got to so far:
from tkinter import *
import tkinter
import tkinter.filedialog
class Window(Frame):
#...
# other functional code
#...
def ChangeCWD(self): #CWD is current working directory
root = tkinter.Tk()
root.withdraw()
directory = filedialog.askdirectory( ... )
root = Tk()
root.mainloop()
However this code produces the error
"NameError: name 'filedialog' is not defined"
when the filedialog.askdirectory() statement is reached. Could anyone provide any help to understand what I should do to correct the situation please?
As an aside, please be gentle with me! I've always been rather mystified by the various ways of invoking import statements and how to use "tk." or "root." before some function calls. There are simply too many conflicting explanations out on the web that I can't get a clear picture.
Upvotes: 1
Views: 762
Reputation: 799
You use import tkinter.filedialog
, which imports tkinter.filedialog
with the namespace tkinter.filedialog
, then you try to use filedialog
in your code.
Pick one of these two:
tkinter.filedialog.askdirectory( ... )
import filedialog from tkinter
, which will import tkinter.filedialog
with the namespace filedialog
.Note: from tkinter import *
might seem like it should import filedialog
, but that *
does not import submodules unless the package has explicitly specified that they should.
Upvotes: 2