简体   繁体   中英

How do I print value of tk slider every time it is updated?

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?

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM