-1

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.

finefoot
  • 9,914
  • 7
  • 59
  • 102
Tim
  • 9
  • 3
  • it sounds like you might be accidently reusing the variable w for something else later on, have you double ckecked your code to make sure thats not the case? – James Kent Apr 09 '15 at 06:16
  • Yea, I made sure that it was the only instance of that var name and it only gives me an error when I use it with that type of function call or a threaded function call. And if I try to use the control variables for the widget they don't work correctly. I can't return the current value of the scale with the control variable or change the current value of the scale with the control variable. – Tim Apr 09 '15 at 06:19
  • Nope! You were correct! I failed to notice one instance. Thank you so much! – Tim Apr 09 '15 at 06:21
  • i know you've said you have your reasons for using global variables in this instance, but this is why it's much better practice to use a class structure and make the widgets 'belong' to the window (or their parent widget) rather than global so it is harder to accidentally overwrite them and you always know what variable is what. – James Kent Apr 09 '15 at 10:53
  • I really do, but I've struggled with learning them so far. That's definitely my next step though. – Tim Apr 09 '15 at 11:04
  • what exactly do you struggle with? there are plenty of good guides to follow, and if you really get stuck you could post a question on here for help with that? – James Kent Apr 09 '15 at 11:09
  • Hey James, thank you so much for your help on this! Got me through my senior project for my bachelors degree in CPE. I've since gotten a much better understanding of class structures. I appreciate your help in seeing me through this issue I had. :) – Tim Oct 27 '22 at 14:18

1 Answers1

1

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!

Tim
  • 9
  • 3