【发布时间】:2016-07-12 21:21:19
【问题描述】:
我希望根据下拉小部件的所选选项更改文本框小部件的描述。我想我可以通过使用列表字典来实现这一点。这是我所做的一个例子:
#Lists of text box descriptions
LEF_SOL = ['AMP','DEP','LAGTIME']
INI_SOL = ['AMP','DEP','XWavemaker']
INI_REC = ['Xc','Yc','WID']
# dictionary made with such lists
wave_options = {'left boundary solitary':LEF_SOL,
'initial solitary wave':INI_SOL,'rectangular hump':INI_REC}
#dropdown widget of the dictionary
wave_maker = widgets.Dropdown(options=wave_options)
# Textbox 1 description = first object of list depending on the dropdown
first_option = widgets.BoundedFloatText(width = "20%",height = '50px',
description = wave_maker.value[0])
# Textbox 2 description = second object of list depending on the dropdown
second_option = widgets.BoundedFloatText(width = "20%",height = '50px',
description = wave_maker.value[1])
# Textbox 3 description = third object of list depending on the dropdown
third_option = widgets.BoundedFloatText(width = "20%",height = '50px',
description = wave_maker.value[2])
display(wave_maker,first_option,second_option,third_option)
然而,当我运行它时,文本框描述仍然是属于出现在下拉列表中的第一个选项的描述。如果下拉列表更改,我希望它们更改。我知道这可以通过“链接”来完成。它应该看起来像这样:
link((wave_maker,'value[0]'), (first_option, 'description'))
link((wave_maker,'value[1]'), (second_option, 'description'))
link((wave_maker,'value[2]'), (third_option, 'description'))
但发生错误:
TypeError: <ipywidgets.widgets.widget_selection.Dropdown
object at 0x7fd47bbbd790> has no trait 'value[0]'
然而,当我将“链接”更改为:
link((wave_maker,'value'), (first_option, 'description'))
然后出现这个错误:
TraitError: The 'description' trait of a BoundedFloatText
instance must be a unicode string, but a value of ['AMP', 'DEP', 'XWavemaker']
<type 'list'> was specified.
有没有办法解决这个问题?
【问题讨论】:
标签: list dictionary widget ipython jupyter