How to get a return from Button?

M

Muddy Coder

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.

Thanks!


Muddy Coder
 
C

Chris Rebert

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top