开发者

Tkinter: Set a 'scale' value without triggering callback?

开发者 https://www.devze.com 2023-01-22 12:23 出处:网络
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 als

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 se开发者_开发百科t 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.


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消