Tkinter: user vs. program events

Fredrik Lundh effbot at telia.com
Wed Feb 23 12:40:29 CET 2000


Andrew M. Kuchling wrote:
> I've been trying to figure out how to distinguish between Tkinter
> events that come from a user and ones that originate from changing the
> value of a widget.  Is this possible?

no.

(unlike event handlers, command callbacks have
no associated event descriptor)

> Here's the motivating example.  I have a Scale widget for indicating
> light intensity:
>
> self.light_scale = Tkinter.Scale(f, command = self.adjust_light)
>
> adjust_light() is a method that takes the current value of the scale
> widget, and sends off a command to change light intensity.  The
> Tkinter program also receives updates on the current state, and
> changes the scale widget to match the current value, with
> self.light_scale.set( <value> ) .
>
> The problem is that the light_scale.set() call causes the associated
> command to be run, so a command is sent off changing the light
> intensity, which causes another status update to be sent out, which
> changes the setting, and so forth.
>
> So, how would you do this in Tkinter?  Can I distinguish between
> events generated by a user and by a .set() call?  Should
> adjust_light() be bound to some different event?  Or should I change
 > the binding of the widget on every update()?

why not just add a semaphore?

    def adjust_light(self):
        if self.status_update:
            return
        ...
    def handle_status_event(self):
        ...
        try:
            self.status_update = 1
            self.light_scale.set(value)
        finally:
            self.status_update = 0
</F>





More information about the Python-list mailing list