1

I'm working with tkinter in python and I have an annoying bug that I can't seem to fix, although the answer is probably obvious.

I am trying to call a dictionary with a string, but for some reason I get the error: Type Error: unhashable type: StringVar. Here is a snippet of code with that issue:

from Tkinter import *
gpa = Tk()

weightsy = {'0': 2, '.1': 6, '.25':.2, '.5':.56, '.75':75, '1':12}
acadw = StringVar()
acadw.set(".1")
print (weightsy.get(acadw)) #Here is the issue; it should return the integer 6.

mainloop()

For extra info, if I remove the tkinter related code (such as the import, gpa = Tk(), StringVar, .set, mainloop()) it works, so I believe it is a tkinter related issue.

1 Answer 1

3

Just as you had to call set method of StringVar object, you also need to call get to retrieve the str data.

print weightsy[acadw.get()]

The dictionary doesn't know to convert your object into a string so it attempts to get the value associated with acadw. You get a TypeError rather than a KeyError since StringVar objects happen to be unhashable.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, makes much more sense now. Still getting used to tkinter.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.