Is there a way to disable a group of radiobuttons associated to the same Tkinter variable?

Question:

I have used a for loop to create a group of radiobuttons that would select an item like so:

modes = [('Item A',20),('Item B',100),('Item C',180),('Item D',280)]
for item in modes:
        button = Radiobutton(picker, text=item[0], variable=item_choice, value=item[0], command=select_item)
        button.place(x = item[1], y = 180)

is there a way to disable these radiobutton via the variable item_choice which they are all associated to? Since I do not have variable names associated to each button. I tried button.config(state=DISABLED) but it does not seem to work. Is there a way to disable all the buttons associated to the variable item_choice ? For further context – I used a loop to create the radiobuttons because in my actual code there is around 10 radiobuttons.

Asked By: pixxelate

||

Answers:

You could append them to a list, and later on iterate over the list of buttons, and disable them.

btnlist = []
for item in modes:
    button = ....
    button.place()
    btnlist.append(button)

And then later…

for button in btnlist:
   button.config(...)

If they are containerized, you could address all child widgets of the container.
If they are in a frame for example:

widgets = frame.winfo_children()
for widget in widgets:
    widget.config(...)

Note: I use widgets in the for loop, you should type check it against buttons if you want to address all buttons. This takes every widget into context.

If you need entry boxes for example, you would check it like so:

if widget.winfo_class() == 'Entry':
Answered By: nigel239
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.