How to get a return from Button?

Chris Rebert clp2 at
Tue Feb 10 21:30:01 CET 2009

On Tue, Feb 10, 2009 at 12:21 PM, Muddy Coder <cosmo_general at> wrote:
> Hi Folks,
> I want to use a Button to trigger askopenfilename() dialog, then I can
> select a file. My short code is below:
> def select_file():
>        filenam = askopenfilename(title='Get the file:')
>       return filenam
> root = Tk()
> Button(root, text='Select a file', command=select_file).pack()
> root.mainloop()
> My goal is to get the path of filenam, but the function select_file()
> has nowhere to return what it selected. Can anybody help me out? I
> consulted the book of Programming Python, but found no demo in this
> regard. If the function has no return, command=blabla will work
> nicely. I am lost in the scope.

The function has nowhere to return its value to (except somewhere in
the innards of Tkinter, where it's discarded), so indeed it should
*not* return. You instead need to modify some program state to hold
the would-be return value.


Follow the path of the Iguana...

More information about the Python-list mailing list