I'm using the Scale widget in Tkinter to control the location of an object in another program. The user can both move the slider in the GUI or move the object directly from the external program. I want the slider value to always show the object's current position. The problem I'm running into is that both moving the slider and programmatically setting the slider with the set
method both call the Scale widget's callback. I'd like to only have it called when it is physically moved.
What I've tried so far:
Following the suggestions from this answer, I tried using a flag and disabling the callback before calling set
, but that didn't quite work in my case. There is a time delay in setting and reading the object's position and I think that is what is causing problems (I built a toy example where the slider was just hooked up to another slider, and the flag solution worked, but it didn't work for my real application). I'm also using the after
function to periodically check whether the physical object is moved. I'm not sure if that is the best way to go or if that affects anything (it might be allowing the flag to be in the wrong state in parts of the code).
I think I'll have to try the suggestion of subclassing the widget, and then making my own version of the set
method that sets a value, but does not call a callback. I'm not sure how to do that though, and from browsing through the source code of the widget it's not too clear where that all happens. Any help would be greatly appreciated, 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 triggers the callback, no matter what I tried to stop it
value.set(0) #<- this works like the previous line but without any callback
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With