5

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 dirname is not a global variable and so is not recognised by UserFileInput. Also how can I bind the button next to the entry field.

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()
218
  • 1,754
  • 7
  • 27
  • 38

1 Answers1

7

Your UserFileInput should return var, not w. Then you can use var.set(dirname) in your askdirectory function which doesn't have to return anything.

I'm not sure however what you try to achieve with text = str(dirname) if dirname else status. Why not just use text = status since dirname can't yet be defined there?

Edit: This should work the way you want it to. The 'print entry text' button shows that you can retreive whatever is in the entry box, either written by the user or put there by the code.

from Tkinter import *
import tkFileDialog

def askdirectory():
  dirname = tkFileDialog.askdirectory()
  if dirname:
    var.set(dirname)

def UserFileInput(status,name):
  optionFrame = Frame(root)
  optionLabel = Label(optionFrame)
  optionLabel["text"] = name
  optionLabel.pack(side=LEFT)
  text = status
  var = StringVar(root)
  var.set(text)
  w = Entry(optionFrame, textvariable= var)
  w.pack(side = LEFT)
  optionFrame.pack()
  return w, var

def Print_entry():
  print var.get()

if __name__ == '__main__':
  root = Tk()

  dirBut = Button(root, text='askdirectory', command = askdirectory)
  dirBut.pack(side = RIGHT)
  getBut = Button(root, text='print entry text', command = Print_entry)
  getBut.pack(side = BOTTOM)

  w, var = UserFileInput("", "Directory")

  root.mainloop()
fhdrsdg
  • 10,297
  • 2
  • 41
  • 62
  • UserFileInput needs to return w so that I can use the output of the function later on. What I was trying to achieve is for `Entry` either to contain the default text given by `status` or to print the output from the `askdirectory` command, once the user has selected the directory. That way the user can either type the directory location in the Entry box, or fill the entry box automatically by selecting the directory location with the mouse. – 218 Aug 13 '14 at 12:12
  • To do what you describe you do not need to return w, but if you feel like you must then return both w and var. Because the Entry uses `textvariable= var`, anything that is written in it can be retreived with `var.get()` and you can put anything you want in it using `var.set('text')`. – fhdrsdg Aug 13 '14 at 13:14
  • I'm afraid this doesn't answer the question. If the Directory box is empty and the user selects a directory via `askdirectory`, the selected directory should propagate to the entry box. This code merely prints the contents of the box to the terminal, which isn't what I was looking for. – 218 Aug 13 '14 at 14:24
  • Have you actually tried the askdirectory button? It puts the selected path in the entry box. The extra print button is just to show that whatever is in the entry box (either written by the user or put in through the button) can be retreived from anywhere. – fhdrsdg Aug 13 '14 at 14:28
  • I have tried the askdirectory button but that doesn't do anything. I assume it puts the chosen directory into the relevant variable but it definitely doesn't put the directory in the entry box in the GUI. This seems like a really obvious thing to want to do. I'm surprised it's so hard. – 218 Aug 13 '14 at 14:36
  • Now that's weird, because the code I posted does exactly that for me. Do you get any error message? And when you click askdirectory, select a directory and then use the print entry text button, does anything get printed? – fhdrsdg Aug 13 '14 at 14:42
  • In transferring your code to mine, I missed the `if` clause in `askdirectory()`. That works perfectly, thanks. – 218 Aug 13 '14 at 15:05
  • Yeah i put that in so nothing happens when you close the `tkFileDialog.askdirectory()` window without selecting a folder. Glad it works now – fhdrsdg Aug 13 '14 at 15:09