1

I'm playing around with Tkinter and it works while I am asking just one input after another. Like 'askopenfilename'. But getting many pop-ups isn't so comfy. I would like to build just one Frame to take all inputs at once.

And so far what I found is just the Frame with buttons (from some tutorials) to ask FileName or Directory, but I can't manage to be able to read the users choice.

import Tkinter, Tkconstants, tkFileDialog
class TkFileDialogExample(Tkinter.Frame):

  def __init__(self, root):
    Tkinter.Frame.__init__(self, root)

    # define buttons
    Tkinter.Button(self, text='askopenfilename', command=self.askopenfilename).pack()
    Tkinter.Button(self, text='askdirectory', command=self.askdirectory).pack()

  def askopenfilename(self):
    return tkFileDialog.askopenfilename()

  def askdirectory(self):
    return tkFileDialog.askdirectory()

if __name__=='__main__':
    root = Tkinter.Tk()
    TkFileDialogExample(root).pack()
    root.mainloop()

And that just builds the frame whith which I am ok, but it loops non stop and I am not able to get what the user have choosen. I have thought maybe I simply could get the value that comes with a function (since it is return) of a button?

Should I necessary have to create an empty list, array, dic to store the values of functions like suggested here: Returning a value after calling a function with a button in Tkinter, which I haven't tried yet...

Or is there other way, just to read it 'from the Buttons'?

Community
  • 1
  • 1
najuste
  • 234
  • 3
  • 15

1 Answers1

2

First, you can allow the user to select multiple files in one dialog by using the multiple flag for tkFileDialog.

Second, tkFileDialog returns the filename (or a string with multiple filenames) and you can do stuff with it if you want. For example:

def askopenfilename(self):
    files = tkFileDialog.askopenfilename(multiple=True)
    # files might be "file1.txt file2.exe file3.bmp" at this point
    if files: # make sure user didn't cancel the dialog, selecting nothing
        for f in files.split(' '):
            print f

If you want to use the selected files in another method, save the filenames to an instance variable, or pass it along directly. Using return isn't much use here, since it's called by the button and it doesn't know what to do with such a return value.

In the approach below, processFiles can use self.files to do whatever you want to do with the files.

def askopenfilename(self):
    self.files = tkFileDialog.askopenfilename(multiple=True)
    self.processFiles()
    # alternatively:
    files = tkFileDialog.askopenfilename(multiple=True)
    self.processFiles(files)

See here for more examples.

Junuxx
  • 14,011
  • 5
  • 41
  • 71
  • writing "Many inputs" I exactly wanted to stress that not multiple. Just files from different locations, different directories. That about the explanation of `return`, seems will try then to save to variables. Already looking at the `processFiles`. Didn't notice such function before! Though I don't want to do much with them, just get the path for reading.. – najuste Nov 20 '12 at 10:46
  • 1
    Sorry, `processFiles` was just an example of another function you could make to do something with the files. It's not a tkinter builtin. – Junuxx Nov 20 '12 at 10:48
  • I'm just not used to create classes.. and so despite my var. definitions and attempts the loop stays running... – najuste Nov 20 '12 at 15:06
  • What loop stays running? It seems fine to me. You mean the tkinter mainloop? See [this question](http://stackoverflow.com/questions/110923/how-do-i-end-a-python-tkinter-program) for that. – Junuxx Nov 20 '12 at 15:47
  • But if quit the loop it asks if I wana kill the script.. Tkinter somehow does not 'accept' choises, maybe it's due to my old 2.5 python, most probable it's just knowledge I lack :) But thanks anyway! – najuste Nov 21 '12 at 07:32