Search code examples
pythontkinterattributeerrortkinter-scale

Tkinter scale widget being called as 'int' in certain cases


I know that global variables are not the best for good programs, but for the purpose of this short programming task I am using them. I want to update a tkinter scale widget inside of a function that is called by a thread or by the binding of a key. When I try to use the scale.set(value) method in either of those functions though, python interprets it as an 'int' for some reason.

In this case there is no problem:

w = tk.Scale(master=frame, from_=50, to=0)
w.set(0)
w.grid(column=1, row=1, sticky=W+N+E+S)

def test(x):
    global w
    w.set(x)

test(17)

But when I try to use the scale instance in a function called by a bound key it gives me this error,

(File "TestingRoverMap.py", line 161, in test
    w.set(x)
AttributeError: 'int' object has no attribute 'set')

when the following code is run:

w = tk.Scale(master=frame, from_=50, to=0)
w.set(0)
w.grid(column=1, row=1, sticky=W+N+E+S)

def upKey(event):
    global w
    w.set(48)

The upKey function is called when you press the up directional key on the keyboard and that code is omitted because I am sure it works perfectly.

Can anyone give me some pointers? I am so lost with this because I've never encountered anything quite like it before.

Thanks.


Solution

  • Turns out that I was reusing 'w' as a value later on in my code so that re-casted the value and gave me the attribute error. This question is solved.

    Thank you to James Kent for helping me with this!