Tkinter: Get value selected, using radiobuttons created with loop

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Tkinter: Get value selected, using radiobuttons created with loop



I'm trying to .get() a radiobutton value from a series of radiobuttons that were created using the loop method.
The problem is, is that all I get is a "0" printed.
I know that this works using individual radiobuttons, but I would like to build a series of radiobutton "questions" using the loop method, and would like to get the value from the radiobuttons created using the loop method.


.get()


Q1 = [
("Text 1", 1),
("Text 2", 2),
("Text 3", 3),
("Text 4", 4),
("Text 5", 5),
]

Label(win, text="Q1. Text here", justify = LEFT, wraplength=500).grid(row=3, columnspan=2)

q1 = IntVar()
for text, value in Q1:
b = Radiobutton(win, text=text, variable=q1, value=value)
b.grid(sticky='W', rowspan=5, columnspan=2)
q1val=q1.get()

def print_button():
print(q1val)

btn = Button(win, text="Print Value", command=print_button).grid(row=99, column=1)





You called q1.get() about a millisecond after creating the IntVar and the buttons, so of course it has no meaningful value yet. You need to make that call in print_button(), after the user has had a chance to select a button.
– jasonharper
Aug 12 at 23:24


q1.get()


print_button()





Well done, it works. Thanks
– Nicolas Smoll
Aug 12 at 23:31









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard