gpt4 book ai didi

jupyter-notebook - 使用ipywidgets水平显示单选按钮

转载 作者:行者123 更新时间:2023-12-04 01:28:37 26 4
gpt4 key购买 nike

我想使用 ipywidgets 水平显示单选按钮。

radio_input1 = widgets.RadioButtons(options=['Option 1', 'Option 2'])

但是它垂直显示单选按钮:

enter image description here

我试图在 HBox 中放置只有一个选项的单选按钮,并向每个单选按钮添加观察者事件,然后从观察者方法中取消选择选定的单选按钮,但在此之前取消观察事件然后重新注册观察事件。它以某种方式调用了 3 次:

output_radio_selected = widgets.Text() # Used to take the user input and access it when needed
radio_input1 = widgets.RadioButtons(options=['Option 1', 'Option 2']) # Declare the set of radio buttons and provide options
radio_input2 = widgets.RadioButtons(options=['Option 3', 'Option 4'])
def bind_selected_to_output(sender): # Connect the input from the user to the output so we can access it
#radio_input1.unobserve(bind_selected_to_output)
radio_input1.unobserve_all()
radio_input1.index=0
#print(sender)
global selected_option # Global variable to hold the user input for reuse in your code
output_radio_selected.value = radio_input1.value
selected_option = output_radio_selected.value # Example variable assigned the selected value
print('Selected option set to: ' + selected_option) # For test purposes
radio_input1.observe(bind_selected_to_output)


radio_input1.observe(bind_selected_to_output) # Run the bind... function when the radio button is changed
#radio_input1.observe(bind_selected_to_output, names=['value'])
#radio_input1 # Display the radio buttons to the user

widgets.HBox([radio_input1])

最佳答案

我可以用可能的 hack 来做到这一点,我不知道它的正确解决方案,但它有效。我拿了 4 个不同的单选按钮和 HBoxwidgets.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])

关于jupyter-notebook - 使用ipywidgets水平显示单选按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61405552/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com