Bokeh取消选中复选框

问题描述 投票:0回答:1

现在,我试图使用自定义按钮取消选中Bokeh复选框。

但是我得到了意想不到的结果:没有在选中状态下创建框,按钮也没有清除复选框。

这是因为我误解了active属性吗?

from bokeh.models.widgets import CheckboxGroup, Button
from bokeh.layouts import column
from bokeh.io import curdoc
from bokeh.plotting import show

checkbox_group_1 = CheckboxGroup(labels=["Group 1 Button"], active=[1])
checkbox_group_2 = CheckboxGroup(labels=["Group 2 Button A", "Group 2 Button B"], active=[1,1])
checkbox_group_3 = CheckboxGroup(labels=["Group 3 Button A", "Group 3 Button B"], active=[1,1])


button = Button(label="Foo", button_type="success")

def buttonclick():
    checkbox_group_1.active = [0]
    checkbox_group_2.active = [0,0]
    checkbox_group_3.active = [0,0]

button.on_click(buttonclick)


layout=column(checkbox_group_1,checkbox_group_2,checkbox_group_3, button)
curdoc().add_root(layout)


!powershell -command {'bokeh serve --show Buttoninteraction.ipynb'}
#I'm working within Jupyter notebook.

理想情况下,当我检查另一个组中的一个时,我想让盒子取消选中。感谢任何帮助。

bokeh
1个回答
1
投票

active属性是一个列表,用于指定所选项的索引。所以要取消选择它们只需使用checkbox_group.active = []

from bokeh.models.widgets import CheckboxGroup, Button
from bokeh.layouts import column
from bokeh.io import curdoc
from bokeh.plotting import show

checkbox_group_1 = CheckboxGroup(labels = ["Group 1 Button"], active = [0])
checkbox_group_2 = CheckboxGroup(labels = ["Group 2 Button A", "Group 2 Button B"], active = [1])
checkbox_group_3 = CheckboxGroup(labels = ["Group 3 Button A", "Group 3 Button B"], active = [1])

button = Button(label = "Foo", button_type = "success")

def buttonclick():
    checkbox_group_1.active = []
    checkbox_group_2.active = []
    checkbox_group_3.active = []

button.on_click(buttonclick)

layout = column(checkbox_group_1, checkbox_group_2, checkbox_group_3, button)
curdoc().add_root(layout)

checkbox_group_2的active值示例:

value      selected
[0]        first 
[1]        second
[0, 1]     both
[]         None
© www.soinside.com 2019 - 2024. All rights reserved.