Python Bokeh:如何循环检查CheckboxButtonGroup
是否有办法在bokeh中的Python Bokeh:如何循环检查CheckboxButtonGroup,python,bokeh,Python,Bokeh,是否有办法在bokeh中的CheckboxButtonGroup中循环浏览按钮对象 我想循环浏览组中的每个按钮,并根据它们的标签在单击时为它们分配不同的处理程序。我设想的是: for button in checkbox_button_group: button.on_click(someHandlerFunc) 但显然,CheckboxButtonGroup对象是不可编辑的。查看文档,我找不到返回组中实际按钮对象的属性。我看到了active属性和标签,但这似乎不是我想要的 在chec
CheckboxButtonGroup
中循环浏览按钮对象
我想循环浏览组中的每个按钮,并根据它们的标签在单击时为它们分配不同的处理程序。我设想的是:
for button in checkbox_button_group:
button.on_click(someHandlerFunc)
但显然,CheckboxButtonGroup对象是不可编辑的。查看文档,我找不到返回组中实际按钮对象的属性。我看到了active
属性和标签,但这似乎不是我想要的
在checkboxgroup回调中,只需获取活动按钮的索引或标签。然后执行if/elif/else语句链,调用您希望与每个按钮关联的任何函数
编辑:以下是按钮组的一些简单功能:
只需单击最后一个按钮即可执行某些操作
from bokeh.io import curdoc
from bokeh.models import CheckboxButtonGroup
a = CheckboxButtonGroup(labels=list('012'),active=[])
def stuff_0(in_active):
if in_active:
print 'do stuff'
else:
print 'undo stuff'
def stuff_1(in_active):
if in_active:
print 'yes'
else:
print 'no'
def stuff_2(in_active):
if in_active:
print 'banana'
else:
print 'apple'
stuff_list = [stuff_0,stuff_1,stuff_2]
def do_stuff(attr,old,new):
print attr,old,new
last_clicked_ID = list(set(old)^set(new))[0] # [0] since there will always be just one different element at a time
print 'last button clicked:', a.labels[last_clicked_ID]
last_clicked_button_stuff = stuff_list[last_clicked_ID]
in_active = last_clicked_ID in new
last_clicked_button_stuff(in_active)
a.on_change('active',do_stuff)
curdoc().add_root(a)
或者,您可以循环浏览按钮,并在每次单击按钮时对所有按钮执行操作:
def do_stuff(attr,old,new):
print attr,old,new
for i in [0,1,2]:
stuff = stuff_list[i]
in_active = i in new
stuff(in_active)
当你说checkboxgroup callback时,你是指callback
属性(我需要在其中创建CustomJS对象?),还是可以在我为checkboxgroup对象设置的on\u click
函数中执行此操作(无法找到如何引用刚刚在on\u click
函数中单击的单个按钮)?@hh程序请查看编辑,您不需要带有复选框按钮组的点击事件。这些并不是真正的按钮,而是显示为按钮的复选框列表,只有一个bokeh模型。您只能与对复选框/单击按钮列表的更改进行交互。