[Tutor] Multiple buttons, One callback (fwd)

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Sat Sep 17 21:58:09 CEST 2005


[Forwarding to tutor, just to keep the mailing list in the loop.  David,
try to use your email client's Reply-To-All feature next time, so that I
don't have to do this.  *grin*]

---------- Forwarded message ----------
Date: Sat, 17 Sep 2005 06:24:03 -0500
From: David Kees <dashifen at dashifen.com>
To: Danny Yoo <dyoo at hkn.eecs.berkeley.edu>
Subject: Re: [Tutor] Multiple buttons, One callback


Actually, that makes a lot of sense.  Feel free to get back to me at
your leisure, I'll be working on this problem some today so I might have
solved it by the time you respond, but if not then I'll probably need
some clarification.

So, just to see if I understand you, I can create a function that
creates the thunk callback and returns it.  This returned thunk callback
(is that a technical term or one of your own, by the way?) can then be
bound as the button's call back and will be able to remember the
parameters passed to the thunker -- the function creating the thunk
callbacks.

That's a great technique, one that I've not run into before.  Without
having applied it yet I can't be sure, but it certainly looks like it'll
do what I need it to.

Thanks,
Dash Kees

Danny Yoo wrote:

>
>
>>I've been able to bind each button to the same callback function (that
>>part's trivial) but what hasn't been trival is determining which button
>>was clicked and, from this information, gathering the correct datum from
>>the list of StringVars which make up the first label text.
>>
>>
>
>
>Hi David,
>
>It's possible to make dynamic callbacks.  For example:
>
>######
>
>
>>>>def some_function(x):
>>>>
>>>>
>...     print "I see", x
>...
>
>
>>>>def make_callback(n):
>>>>
>>>>
>...     def callback():
>...         return some_function(n)
>...     return callback
>######
>
>
>make_callback() is a function that returns a "thunk" callback.  That
>returned thunk doesn't take an argument, so it's perfectly appropriate as
>a button callback.
>
>And yet, that callback can remember what was initially passed into
>make_callback() --- it remembers the datum that we used to build the
>thunk.  Here, take a look:
>
>######
>
>
>>>>callbacks = [make_callback(i) for i in range(5)]
>>>>callbacks[0]
>>>>
>>>>
><function callback at 0x403a610c>
>
>
>>>>callbacks[0]()
>>>>
>>>>
>I see 0
>
>
>>>>callbacks[1]
>>>>
>>>>
><function callback at 0x403a6bfc>
>
>
>>>>callbacks[1]()
>>>>
>>>>
>I see 1
>######
>
>Does this make sense?  Please free to ask questions about this.
>
>
>Good luck!
>
>
>
>
>
>





More information about the Tutor mailing list