Hello,

I'm using a askdialog box from tkinter with a form. My problem is when I use "print" or "return" to get the selected directory it prints it to the terminal (if open) and I would like it to print to an Entry textbox. For example:

from Tkinter import *
from tkFileDialog import askdirectory

root = Tk()

e1 = Entry(root)

def browser():
dir1 = askdirectory(title="Select A Folder", mustexist=1)
if len(dir1) > 0:
	print dir1    ###<----------I would like this "dir1" to print into the "e1" Entry above

Button(root, text='Browse', command=browser)

root.mainloop()

I feel like this should be pretty easy but can't seem a way to figure it out. I'm new to Python and taking the learn-as-I-go approach. Any help would be much appreciated.

- RagS

You want to use .set() but it must be with a Tkinter control variable. Look for the paragraph that starts with "For example, you could link an Entry widget to a Label widget" and the "Entry" paragraph at this link. The New Mexico Tech Tkinter pages are well done.
http://infohost.nmt.edu/tcc/help/pubs/tkinter/control-variables.html

Here is one way to do this ...

from Tkinter import *
from tkFileDialog import askdirectory

def browser():
    dir1 = askdirectory(title="Select A Folder", mustexist=1)
    if len(dir1):
        #print dir1
        e1.insert(0, dir1)

root = Tk()

e1 = Entry(root)
e1.pack()
b1 = Button(root, text='Browse', command=browser)
b1.pack()

root.mainloop()

Thanks you guys for such a quick response. It totally worked!!! I couldn't get the insert() to work but I'm sure it was just a mistake on my end. Setting the control variable and using get() and set() did the trick. If only all forums worked this well...

- RagS

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.