6

I have a Tkinter GUI where there is a Scale object. I have a callback assigned (by the command constructor parameter) to perform an action when the user changes the scale position. However, there is also a case where the value represented by the scale is modified externally, and so I set the scale position using Scale.set(). In this case, I want to set the scale, but not trigger the callback, since the rest of the program already knows about the change. However, I notice that the callback is indeed triggered by set().

Is it possible to do one of:

  1. Set the scale value without triggering the callback.

  2. Differentiate in the callback whether it was triggered by user interaction or by Scale.set() being called.

Thanks.

1
  • Two would seem the more likely option. Commented Oct 27, 2010 at 23:52

2 Answers 2

6

I was faced with the same issue and neither inhibiting the callback nor setting a global variable to check the callback status worked for me, for some obscure reason the callback kept being called after my code had finished running.

Despite this, the solution that worked for me is simple : I used a variable in the widget and set the value using the set method of the variable rather than the one of the widget.

value = DoubleVar()
scale = Scale(master, variable=value, command=callback)
scale.pack()

scale.set(0) #<- this triggered the callback, no matter what I tried to stop it
value.set(0) #<- this seems to work like the previous line but without any callback
Sign up to request clarification or add additional context in comments.

1 Comment

I've been fighting the same problem and had the same issues you did. Your solution worked perfectly for me.
3

There is nothing specifically built-in to Tkinter to solve this. It's really a simple problem to solve though: remove the callback, set the value, add the callback. Or, set a global flag and check for that flag in the callback.

There are ways to solve the problem -- subclass the widget, for example -- but that doesn't really buy you anything. Just go with the simple solution and move on to more interesting problems.

3 Comments

Okay, I'll probably use the flag approach. Thanks.
how do you remove/readd the callback?
@moefear: every widget has a configure method you can use to change the configured options after the widget has been created.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.