我正在尝试创建一个工具,允许用户选择不同的参数来绘制模型数据。我最初可以生成小部件,但我想根据用户的可能选择更新其中一个小部件。
在这个例子中,我将值“Phase”默认为“Water”和“Effective Radius”为一个与仅与水相对应的值列表。如果用户从“阶段”中选择“Ice”,我希望窗口小部件从不同的值列表中更新。这是相关代码:
from bokeh.models.widgets import (Slider, Button, CheckboxGroup,
RadioButtonGroup, RadioGroup, Dropdown, Select)
# Selector for phase.
#--------------------
phs = [ "Water", "Ice", "Clear" ]
phs_select1 = Select( title="Cloud Phase", value=phs[0], options=phs,
width=150 )
phs_select2 = Select( title="Cloud Phase", value=phs[0], options=phs,
width=150 )
def update_phs( flag ):
if flag == 0:
fname[flag,3] = phs_select1.value.lower()
update_de_list( )
de_select1.update()
if flag ==1:
fname[flag,3] = phs_select2.value.lower()
update_de_list( )
phs_select1.on_change( 'value', lambda attr, new, old: update_phs(0) )
phs_select2.on_change( 'value', lambda attr, new, old: update_phs(1) )
# Selector for effective diameter.
#----------------------------
de = [np.str(x) for x in (8, 20, 32, 50)]
def update_de_list():
# Add something here to update the widget?
?????????????????????????
if phs_select1.value.lower() == "water":
de = [np.str(x) for x in (8, 20, 32, 50)]
elif phs_select1.value.lower() == "ice":
de = [np.str(x) for x in (21.86, 46.34, 115.32)]
????????????????????????????????????
?
? Once user has selected a different phase, I need to update
? "Effective Diameter field.
?
?????????????????????????
de_select1= Select( title="Effective Diameter", value=de[0], options=de,
width=150 )
de_select2 = Select( title="Effective Diameter", value=de[0], options=de,
width=150 )
def update_de( flag ):
if flag == 0:
fname[flag,5] = "de"+de_select1.value
if flag == 1:
fname[flag,5] = "de"+de_select2.value
de_select1.on_change( 'value', lambda attr, new, old: update_de(0) )
de_select2.on_change( 'value', lambda attr, new, old: update_de(1) )
# Layout the widgets for the first file
lft_pnl = [srf_select1,igbp_select1,tau_select1,ws_select1,aod_select1]
rgt_pnl = [ prf_select1, phs_select1, de_select1, cld_select1 ]
lft_col = WidgetBox( *lft_pnl, sizing_mode="fixed", width=150)
rgt_col = WidgetBox( *rgt_pnl, sizing_mode="fixed", width=150)
# Layout the widgets for the first file
lft_pnl = [srf_select2,igbp_select2,tau_select2,ws_select2,aod_select2]
rgt_pnl = [ prf_select2, phs_select2, de_select2, cld_select2 ]
lft_col2 = WidgetBox( *lft_pnl, sizing_mode="fixed", width=150)
rgt_col2 = WidgetBox( *rgt_pnl, sizing_mode="fixed", width=150)
pnl_layout = layout([[lft_col, rgt_col],[lft_col2,rgt_col2],[plt_button]])
l = layout(children=[[grid,pnl_layout]])
curdoc().add_root( l )
您的示例中缺少一些代码,所以我希望我能正确理解您的问题:)。据我所知,当用户在“云阶段”字段中更改选择时,您希望更改“有效直径”字段中的选项。我想要实现你想要的,你可以在update_de_list()回调中替换以下行:
de = [np.str(x) for x in (21.86, 46.34, 115.32)]
有:
de_select1.options = [np.str(x) for x in (21.86, 46.34, 115.32)]
de_select2.options = [np.str(x) for x in (21.86, 46.34, 115.32)]