Does changing a Tkinter Variable bound to a widget invoke its 'command' callback function?

execrable at gmail.com execrable at gmail.com
Thu Nov 29 05:11:53 EST 2007


I am writing a simple (maybe a bit conveluded, but I just started
learning Tkinter) program that uses a 'notebook' code snippet from a
python cookbook.  It creates a customizable frame and radio button for
each 'tab' and uses .forget to hide the frames that aren't 'current'.
I modified it to use a Scale slider bar instead of radio buttons.

What my program does is create a parent notebook object, and in each
notebook frame, child notebooks are created.  All the sliders are
created with:

 command=lambda x: self.display(int(x))

Which displays the contents of the associated notebook frame (via
forget/pack).  All of this also works fine, but I wanted it to be a
little more user-friendly by linking all the sliders on the child
notebooks using a control IntVar so that each child notebook can be on
the same page while scrolling through them.  The problem is that when
the currently displayed slider is moved, it updates the current child
notebook, but not the notebooks of the hidden frames.  Their sliders
are moved when they are next displayed, but the associated frames are
not updated, so there is a mismatch between slider and frame until
that specific slider is moved.

I can't find much information on control variables with respect to
Scale widgets, so any insight, hints, or suggestions would be greatly
appreciated.

Some snippets of the involved routines:

    def __init__(self, master, size, start=0, side=LEFT, choice=None):

        self.active_fr = start
        self.size = size
        if(choice):
            self.choice = choice
        else:
            self.choice = IntVar(value=start)

...

# create slider
        self.slide = Scale(self.rb_fr, variable=self.choice,
orient=self.orient,
                           from_=start, to=(size-start+1), length=200,
                           command=lambda x: self.display(int(x)))
...

    def display(self, fr):
        self.frames[self.active_fr].forget()
        self.frames[fr].pack(fill=BOTH, expand=1)
        self.active_fr = fr



More information about the Python-list mailing list