[Tkinter-discuss] Scale widget fires command on entering mainloop?

Guilherme Polo ggpolo at gmail.com
Fri Nov 7 15:23:48 CET 2008

On Fri, Nov 7, 2008 at 11:18 AM,  <david.giesen at kodak.com> wrote:
> Hi -
> I'm trying to figure out why the Tkinter Scale widget seems to fire its
> command callback when the root.mainloop() is executed.  This seems in
> contrast with other widgets like the Button that only fire the command
> callback in response to user interaction.  I'm using Windows XP, the
> Python 2.5 and, based on the folder names in the Python\tcl folder, tk8.4
> and tcl8.4.
> Below is an example script.  It sets up two sliders who get values
> initialized in two different ways.  The print statements show that the
> sliders are initialized and packed, and THEN the command callbacks occur
> once the root.mainloop() statement executes.
> Can suggest a way to prevent this from happening?  I could have each
> callback do no action the first time it is called, I suppose.  But I
> wonder if I'm simply doing something wrong, or if I'm assuming a behavior
> from Tkinter that I should not, or if I've encountered something
> "strange".
> Thanks in advance!
> Dave Giesen
> # Start of demo script
> import Tkinter as Tk
> import gwidgets as gw
> root = Tk.Tk()
> def printme1(value):
>        print 'Scale # 1 just called back with value:', value
> scalevar = gw.iVar(7)
> scale1 = Tk.Scale(root, command=printme1, variable=scalevar)
> print 'Scale #1 initialized with value:', scale1.get()
> def printme2(value):
>        print 'Scale # 2 just called back with value:', value
> scale2 = Tk.Scale(root)
> print 'Scale #2 initialized with value: ', scale2.get()
> scale2.set(4)
> print 'Scale #2 just set to value: ', scale2.get()
> scale2.configure(command=printme2)
> print 'Now packing scales'
> scale1.pack()
> scale2.pack()
> print 'Done packing scales'
> root.mainloop()
> # End of demo script

Comparing this Scale example with a command set and a Button with a
command set is unfair. When you change Scale's value, the scale
eventually has to be redraw.

So, you create a Scale with the "command" option set, then you change
its value by calling scale.set, then you run mainloop. When mainloop
runs the scheduled events start firing, and there is one around there
saying your scale changed its value so the slider has to be redrawn.
But you also set a command, so, since the scale changed now it must be
invoked, and the callback is called.

Can you explain why is it a problem to invoke the callback if the
scale has changed ? And why don't you want it to be called the first

-- Guilherme H. Polo Goncalves

More information about the Tkinter-discuss mailing list