Mazzone
Mazzone

Reputation: 308

Python Tkinter - cannot select directory (tkFileDialog not found)

We're trying to store a directory path in a variable using Tkinter's tkFileDialog, and it won't work (details later).

from Tkinter import *
import os
from tkFileDialog import askopenfilename, askdirectory  
# Create the window
root = Tk()

# Application title & size
root.title("Title")
root.geometry("1000x600")

# Creating frame to add things to
app = Frame(root) 
app.grid() # Adding app frame to grid  

# Method that opens file chooser 
# Gets used when button is clicked (command)
def openFileBox():
    directoryPicked = tkFileDialog.askdirectory()
    #easygui.fileopenbox()
    for filePicked in os.listdir(directoryPicked):
        if filePicked.lower().endswith(".jpg") or filePicked.lower().endswith(".gif") or filePicked.lower().endswith(".png"):
            print filePicked

#TODO: add button 'Select Folder'
loaderButton = Button(app)
loaderButton["text"] = "Select Folder"
loaderButton["command"] = openFileBox
loaderButton.grid()  
# Tells the program to run everything above
root.mainloop()  

So what needs to happen? The way we see it (and we're beginners looking for feedback here), it should be running the openFileBox method when the button is pressed. When the method runs, it should store a selected directory to directoryPicked and print it to the console just to be sure it's working, but when we press the button it simply says 'tkFileDialog' is not defined.
Any thoughts?

Upvotes: 1

Views: 582

Answers (1)

Steven Summers
Steven Summers

Reputation: 5384

It's because you're only importing askopenfilename, askdirectory from tkFileDialog you're not actually importing tkFileDialog itself

So you need to change directoryPicked = tkFileDialog.askdirectory() to directoryPicked = askdirectory()

Upvotes: 1

Related Questions