How to get a return from Button?

Discussion in 'Python' started by Muddy Coder, Feb 10, 2009.

  1. Muddy Coder

    Muddy Coder Guest

    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
    Muddy Coder, Feb 10, 2009
    #1
    1. Advertising

  2. Muddy Coder

    Chris Rebert Guest

    On Tue, Feb 10, 2009 at 12:21 PM, Muddy Coder <> 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
    Chris Rebert, Feb 10, 2009
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. PvdK
    Replies:
    0
    Views:
    2,978
  2. Fredrik Lundh
    Replies:
    0
    Views:
    383
    Fredrik Lundh
    Jul 6, 2006
  3. Steve Holden
    Replies:
    0
    Views:
    380
    Steve Holden
    Jul 14, 2006
  4. Greenhorn
    Replies:
    15
    Views:
    814
    Keith Thompson
    Mar 6, 2005
  5. albert kao
    Replies:
    1
    Views:
    2,015
    marisa lee
    Apr 10, 2010
Loading...

Share This Page