Problem with using dictionary as a variable with radiobuttons in tkinter

17 Views Asked by At

When I try assigning a dictionary item as a radiobutton variable after defining it as a type tk.Stringvar(), the get function returns PY_VAR0 rather than the string it was assigned.

###################################################

root = tk.Tk()

variables = dict()
variables['Language']=tk.StringVar()

def cb():
    print(variables.get('Language'))
    

for Lang in ('English', 'German', 'French'):
    tk.Radiobutton(root, text=Lang, variable=variables['Language'], value=Lang, command=cb).pack()

root.mainloop()

If I substitute the dictionary with a single variable the code works as expected.

##########################################################

root = tk.Tk()

Language=tk.StringVar()

def cb():
    print(Language.get())
    

for Lang in ('English', 'German', 'French'):
    tk.Radiobutton(root, text=Lang, variable=Language, value=Lang, command=cb).pack()

root.mainloop()
1

There are 1 best solutions below

0
CobraJet390 On

Found the problem, need to call out the dictionary key before the get() method is used.

i.e. variables['Language'].get()