How to get a return from Button?

Chris Rebert clp2 at rebertia.com
Tue Feb 10 15:30:01 EST 2009


On Tue, Feb 10, 2009 at 12:21 PM, Muddy Coder <cosmo_general at yahoo.com> 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.

Cheers,
Chris

-- 
Follow the path of the Iguana...
http://rebertia.com



More information about the Python-list mailing list