Mikhail_Sam
Mikhail_Sam

Reputation: 11218

Uncheck other checkbox on one checked

I wrote this code (actually using Jupyter-Notebook):

import random
from ipywidgets import widgets


for ind in range(0,10):
    print('Some text'+str(ind))
    cb_inner = []
    a = random.randint(0,3)
    for el in range(0,3):
        cb_value = True if el == a else False
        A = widgets.Checkbox(value=cb_value, description='cb'+str(el))
        cb_inner.append(A)
    for_box_list = cb_inner
    box = widgets.HBox(for_box_list)
    display(box)
    print('---'*41)

Now I have text strings and line of chackboxes for each. Only one checkbox active at the time. If I check another checkbox both of them become checked. What I want to do: if I check another checkbox, first one become disabled automatically (for group of checkboxes for every text message).

I suppose I need to create event for this. I found this: Python ipywidgets checkbox events

But I can't understand how to implement it for all my boxes?

Upvotes: 0

Views: 1240

Answers (1)

Mikhail_Sam
Mikhail_Sam

Reputation: 11218

I thought a lot about list of checkboxes and creating events for them in a loop, but finally found simple way: using RadioButtons. To handle the situation where all checkboxes in a line are turned off, I just added one more option in RadioButton arguments:

from ipywidgets import widgets
buttons_list = []
for ind in range(0,10):
    print('My message',ind)
    rb = widgets.RadioButtons(options=['cb1', 'cb2', 'noone checked'],
                          value='cb1')
    display(rb)
print('---'*41)
buttons_list.append(rb)

Upvotes: 1

Related Questions