Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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':
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading