Я мог бы сделать это с возможным взломом, я не знаю правильное решение, но это работает. Я взял 4 разных переключателя и виджеты HBox. HBox ([radio1, radio2, radio3, radio4]) Затем после выбора одного переключателя я отменяю выбор другого переключателя, который выбран. Вот как я это сделал:
import ipywidgets as widgets
import numpy
output_radio_selected = widgets.Text()
radio1 = widgets.RadioButtons(options=['Option 1'])
radio2 = widgets.RadioButtons(options=['Option 2'])
radio3 = widgets.RadioButtons(options=['Option 3'])
radio4 = widgets.RadioButtons(options=['Option 4'])
radio1.index = None
radio2.index = None
radio3.index = None
radio4.index = None
def radio1_observer(sender):
#print(sender)
radio2.unobserve(radio2_observer, names=['value'])
radio2.index = None
radio3.unobserve(radio3_observer, names=['value'])
radio3.index = None
radio4.unobserve(radio4_observer, names=['value'])
radio4.index = None
global selected_option
output_radio_selected.value = radio1.value
selected_option = output_radio_selected.value
print('Selected option set to: ' + selected_option)
radio2.observe(radio2_observer, names=['value'])
radio3.observe(radio3_observer, names=['value'])
radio4.observe(radio4_observer, names=['value'])
def radio2_observer(sender):
radio1.unobserve(radio1_observer, names=['value'])
radio1.index = None
radio3.unobserve(radio3_observer, names=['value'])
radio3.index = None
radio4.unobserve(radio4_observer, names=['value'])
radio4.index = None
global selected_option2
output_radio_selected.value = radio2.value
selected_option2 = output_radio_selected.value
print('Selected option set to: ' + selected_option2)
radio1.observe(radio1_observer, names=['value'])
radio3.observe(radio3_observer, names=['value'])
radio4.observe(radio4_observer, names=['value'])
def radio3_observer(sender):
radio1.unobserve(radio1_observer, names=['value'])
radio1.index = None
radio2.unobserve(radio2_observer, names=['value'])
radio2.index = None
radio4.unobserve(radio4_observer, names=['value'])
radio4.index = None
global selected_option3
output_radio_selected.value = radio3.value
selected_option3 = output_radio_selected.value
print('Selected option set to: ' + selected_option3)
radio1.observe(radio1_observer, names=['value'])
radio2.observe(radio2_observer, names=['value'])
radio4.observe(radio4_observer, names=['value'])
def radio4_observer(sender):
radio1.unobserve(radio1_observer, names=['value'])
radio1.index = None
radio2.unobserve(radio2_observer, names=['value'])
radio2.index = None
radio3.unobserve(radio3_observer, names=['value'])
radio3.index = None
global selected_option4
output_radio_selected.value = radio4.value
selected_option4 = output_radio_selected.value
print('Selected option set to: ' + selected_option4)
radio1.observe(radio1_observer, names=['value'])
radio2.observe(radio2_observer, names=['value'])
radio3.observe(radio3_observer, names=['value'])
radio1.observe(radio1_observer, names=['value'])
radio2.observe(radio2_observer, names=['value'])
radio3.observe(radio3_observer, names=['value'])
radio4.observe(radio4_observer, names=['value'])
widgets.HBox([radio1,radio2,radio3,radio4])