how to return value from button clicked by python

Tim Roberts timr at probo.com
Sat Sep 12 06:57:26 CEST 2009


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.
>    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.
-- 
Tim Roberts, timr at probo.com
Providenza & Boekelheide, Inc.



More information about the Python-list mailing list