[Tkinter-discuss] tkinter askdirectory print

Martin Franklin mfranklin1 at gatwick.westerngeco.slb.com
Mon Feb 18 10:23:15 CET 2008


RagZ wrote:
> Hello,
> 
>   I'm writing an application with a browse button that opens up the
> "askdirectory" box to choose a directory. The problem is when the directory
> is chosen and the ok button is hit it prints the directory to the terminal
> (if it's open) and I would like it to print it to a ENTRY() that I have. For
> example:
> 
> from Tkinter import *
> from tkFileDialog import askdirectory
> 
> root = Tk()
> 
> e1 = Entry(root)                #<---------I would like the dir1 to print
> here 
> 
> def browser():
>     dir1 = askdirectory(parent=root, title="Select A Folder", mustexist=1)
>     if len(dir1) > 0:
> 	print dir1
> 
> Button(root, text="Browse", command=browser)
> 
> root.mainloop()
> 
> So from this what I'm asking is how can I get dir1 to print to e1 (the entry
> box)? I'm new to Python and am taking the learn-as-I-code route but can't
> seem to figure this one out. It might be really easy but I just don't see
> it. Thanks for any help.
> 
>                                                                                                          
> - RagZ

You are almost there
## not tested, but *should* work ;)

from Tkinter import *
from tkFileDialog import askdirectory

root = Tk()


## use a Tkinter.StringVar with and Tkinter Entry widget
d1 = StringVar()
e1 = Entry(root, textvariable=d1)
e1.pack()

def browser():
   dir1 = askdirectory()
   if dir1:
     d1.set(dir1)

b1 = Button(root, text="Browse", command=browser)
b1.pack()

root.mainloop()


Cheers
Martin


















-- 
signature file not found, must be something I ate


More information about the Tkinter-discuss mailing list