Tkinter question

Peter Otten __peter__ at web.de
Wed Jun 15 20:13:59 CEST 2005


Fredrik Lundh wrote:

> note that you're assigning all buttons to the same instance variable, so
> even if the above did work, it would always return a plus.
> 
> since the command callback doesn't know what widget it was called
> from, you need to create a separate callback for each widget.  here's
> one way to do that:
> 
> for x in range(4):
>     for y in range(4):
>         text = i[t] # label text
>         def callback():
>             self.pressed(text)

Change the above function to

          def callback(text=text): 
              self.pressed(text)

Otherwise you have distinct callbacks, but they are still all seeing the
same 'text' variable, probably bound to "+" by the time you press a button.

Peter




More information about the Python-list mailing list