title: How can I use the output from tkFileDialog.askdirectory to fill a tkinter Entry box
body: <p>I have a tkinter Entry box in which the user can insert a path to the directory. Alternatively the user can click a button to select the directory. How can I set the output from the button to fill the Entry box? I have tried the following but <code>dirname</code> is not a global variable and so is not recognised by <code>UserFileInput</code>. Also how can I bind the button next to the entry field.</p>
  
  <pre><code>from Tkinter import *
  import tkFileDialog
  
  def askdirectory():
    dirname = tkFileDialog.askdirectory()
    return dirname
  
  def UserFileInput(status,name):
    optionFrame = Frame(root)
    optionLabel = Label(optionFrame)
    optionLabel["text"] = name
    optionLabel.pack(side=LEFT)
    text = str(dirname) if dirname else status
    var = StringVar(root)
    var.set(text)
    w = Entry(optionFrame, textvariable= var)
    w.pack(side = LEFT)
    optionFrame.pack()
    return w
  
  
  if __name__ == '__main__':
    root = Tk()
  
  
    dirBut = Button(root, text='askdirectory', command = askdirectory)
    dirBut.pack(side = RIGHT)
  
    directory = UserFileInput("", "Directory")
  
  
    root.mainloop()
  </code></pre>