Javier Valadez Javier Valadez - 1 year ago 187
Python Question

Tkinter Scale - Python

I'm working with with Tkinter Scale 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.

Answer Source

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):
    if(value >= 10):
        print "The value is ten"

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



>>> '0' >= 10

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()
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download