Javier Valadez
Javier Valadez

Reputation: 38

How to do an action for certain values of tkinter Scale widget?

I'm working with with Tkinter in Python and I have an issue with the Scale widget. What I want to do is an action for certain values of the Scale.

Here is the part of the Scale code:

self.scale = Scale(from_=0, to=100, tickinterval=20, orient=HORIZONTAL, command= self.scale_onChange)

def scale_onChange(self, value):
    if(value >= 10):
        print "The value is ten"

Something strange is happening, when I run the script, the scale value is 0 nevertheless the condition seems true and print "The value is ten". Also when I change the value of the scale never matches the condition even if the value is greater than 10.

Upvotes: 0

Views: 483

Answers (1)

You have a type mismatch. value is a string not a numeric type, and in Python 2.* '0' is greater than 10. Thanks to Tadhg McDonald-Jensen for pointing out that this sort of silent error is specific to Python 2.*.

from Tkinter import *

def scale_onChange(value):
    print(value)
    print(type(value))
    if(value >= 10):
        print "The value is ten"

master = Tk()
scale = Scale(from_=0, to=100, tickinterval=20, orient=HORIZONTAL, command=scale_onChange)
scale.pack()

mainloop()

e.g.

>>> '0' >= 10
True

in Python 3.* you'd have gotten an error:

>>> '0' >= 10
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unorderable types: str() >= int()

Upvotes: 1

Related Questions