Search code examples
pythontkinterwidgetslidertkinter-scale

How to update a Scale widget with the value from another Scale widget


I'm trying to update the value of a Scale widget dynamically so it changes its value to the value of another Scale widget. Just to understand how the Scale.set() method works I've tried something like this:

from Tkinter import *

def sel():
   selection = "Value = " + str(varX.get())
   label.config(text = selection)

def test():
    toScale = varX.get()
    scaleY.set(toScale)   


root = Tk()
varX = DoubleVar()
varY = DoubleVar()

scaleX = Scale(root, variable = varX, command=test)
scaleX.pack(anchor=CENTER)

scaleY = Scale(root, variable = varY)
scaleY.pack()

button = Button(root, text="Get Scale Value")
button.pack(anchor=CENTER)

label = Label(root)
label.pack()

txt = Entry(root)
txt.pack()

root.mainloop()

But when I try to run that I got:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Python27\lib\lib-tk\Tkinter.py", line 1486, in __call__
    return self.func(*args)
TypeError: test() takes no arguments (1 given)

And when I pass the command keyword argument to the button it works fine. What is the problem here? It's all about the Scale widget and its methods I think. I'm not too comfortable with them and any helps will be appreciated.


Solution

  • Scale sends new value to function

    def test(value):
        scaleY.set(value)
    

    I think you can use one variable to get the same result

    one_variable = DoubleVar()
    
    scaleX = Scale(root, variable=one_variable)
    scaleY = Scale(root, variable=one_variable)