how to return value from button clicked by python

Hendrik van Rooyen hendrik at microcorp.co.za
Sat Sep 12 08:49:15 EDT 2009


On Saturday 12 September 2009 06:57:26 Tim Roberts wrote:
> chen tao <ct19850519 at gmail.com> wrote:
> >     I have several buttons, I want to realize: when I click first
> >button, the button will call a function, and the function should
> >return some parameter value, because I need this value for the other
> >buttons.

As Tim said - you cannot return anything, as the button command is called by 
the main GUI loop.

If you really want to, you can set a variable global to the class, (Like 
self.my_passed_param) and use it in the second button - however then you have 
to handle cases where the user does not click the buttons in the sequence you 
expect.

> >    I tried the button.invoke() function, it almost got it...however,
> >I only want it returns value when the button clicked, but because the
> >program is in the class _ini_ function, so it always runs once before
> >I click the button...
> >    Any one can give me some suggestions?
>
> You're thinking of your program in the wrong way.  When you write a GUI,
> things don't happen in order.  Your __init__ function merely sets up your
> window structure.  The window has not actually been created or displayed at
> that time.
>
> Later, when you call the "mainloop" function, it will start to process
> messages.  Your window will be displayed, and then you'll go idle while
> waiting for user input.  When you click a button, the mainloop will call
> your handler, do a little processing, and return.
>
> So, your button function can't really return anything.  There's nothing to
> return it TO.  If there is some action you need to take when the button is
> clicked, then you DO that function in the button handler.

This is perfectly right.

- Hendrik




More information about the Python-list mailing list