Tkinter: Get value selected, using radiobuttons created with loop
Clash 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)
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.
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 inprint_button()
, after the user has had a chance to select a button.– jasonharper
Aug 12 at 23:24