1

I have a tk slider in my python program and I am trying to get its value every time I move it. Program does read out the initial value of the slider, but that is all that gets read out. Here is my code:

valuelist[50, 100, 150, 200]
class MainApp(Frame):
    def valuecheck(val, self, scale):
        scalepic = scale
        print scalepic.get()
    def createControls(self):
        scalepic = Scale(self, from_=min(valuelist), to=max(valuelist), resolution = 25, orient=HORIZONTAL)
        scalepic.set(100)
        scalepic.configure(command = self.valuecheck(val, scalepic))
        scalepic.pack()
    def __init__(self, parent):
        Frame.__init__(self, parent, width = 800, height = 600)
        self.pack()
        self.createControls()

I tried adding this to the body of valuecheck:

newval = min(valueList, key=lambda x:abs(x-float(val)))
scalepic.set(newval)
print newval

and got rid of resolution option when defining scalepic. I also tried passing scalepic.get() as an argument for val. I also tried removing val as an argument in valuecheck What else should I try?

  • You are calling `valuecheck` ONCE, during initial configuration of the slider. You need to pass a function, not the result of calling a function, as the `command=` option. – jasonharper Sep 24 '18 at 01:01
  • That makes sense, thanks. How would I pass the value check function in this case? Also, in general, how do you call a function every time a widget is interacted with? – Eric Lopez-Reyes Sep 24 '18 at 02:25
  • Possible duplicate of [Why is Button parameter “command” executed when declared?](https://stackoverflow.com/questions/5767228/why-is-button-parameter-command-executed-when-declared) – fhdrsdg Sep 24 '18 at 06:40

1 Answers1

0

Don't assign the function to the command. Use lambda.

scalepic.configure(command = lambda :self.valuecheck(scalepic))

But also edit your function:

def valuecheck(self, scale):

Because there is no need for val.

Nouman
  • 6,947
  • 7
  • 32
  • 60