ANN: Thinking in Tkinter

sismex01 at sismex01 at
Thu Sep 12 19:51:09 CEST 2002

> True. It would help if the command callback sent an arguement
> that identified the widget (making it look a bit more like an
> event binding). In any event, it doesn't and command is
> unfortunately still the best way to handle button widgets.

Do something like this:

# This is a kinda "ordinary" callback class.
class Callback:
  def __init__(self, function, *args, **kw):
    self._args = args
    self._kw = kw
    self._func = function
    self._button = button
  def __call__(self):
    return self._func(*self._args, *self._kw)

# And, when you define the button, do something like:
btn = Tkinter.Button(someframe, label="huuuge peeectorall muscels")
btn["command"] = Callback(some_func, btn, any-other-args)

# When "btn" is clicked on, the first argument in some_func
# will be btn.

Good luck!



-- please disregard offensive and arrogant legalese. :-(

> In general Tkinter has several useful callback mechanisms, 
> each of which 
> is especially easy to use for a particular type of problem 
> and may be a 
> headache for other sorts of problems (e.g. events to simulate button 
> command). Examples (aside from Button command):
> Event binding examples:
> - Use mouse button events to do useful things in the Canvas 
> widget. For 
> instance you can easily drag objects around by binding to 
> <Button-1> and 
> <B1-Motion>. Or show information when the pointer is near an 
> object on a 
> Canvas using <Motion>.
> - Use keyboard events to modify the behavior of Entry widgets, for 
> example bind to the return keyto accept the data and use it 
> somehow, or 
> bind to up-arrow and down-arrow to implement a history mechanism.
> Variable trace:
> - To implement input validation in Entry widgets, specify a 
> textvar and 
> put a trace on the variable. In your callback beep and 
> restore the old 
> value of the variable if the proposed new value is invalid.
> There are others. My Tkinter Summary 
> <> section 
> "Events and Callbacks" has what I believe is a complete set though 
> without examples. I'm should take another look at it and 
> expand it a bit.
> -- Russell
> -- 

La informacion contenida en este mensaje es confidencial y restringida y
esta destinada unicamente para el uso de la persona arriba indicada, Esta
comunicacion representa la opinion personal del remitente y no refleja
necesariamente la opinion de la Compañia. Se le notifica que esta
estrictamente prohibida cualquier difusion, distribucion o copia de este
mensaje. Si ha recibido esta comunicacion o copia de este mensaje por error,
o si hay problemas en la transmision, favor de comunicarse con el remitente.

Todo el correo electrónico enviado para o desde esta dirección será
procesado por el sistema de correo corporativo de HEB. Tal correo
electrónico esta sujeto a ser almacenado y puede ser revisado por alguien
ajeno al recipiente autorizado con el propósito de monitorear que se cumplan
las normas de seguridad de la empresa.

More information about the Python-list mailing list