【问题标题】:How to change the description of textbox widgets depending on the chosen option of a dropdown widget? (Dictionary of Lists)如何根据下拉小部件的所选选项更改文本框小部件的描述? (列表字典)
【发布时间】: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


    【解决方案1】:

    我发现可以通过使用 traitlets“观察”或“交互”来解决我的问题。我附上了一个“观察”的例子:

    wave_options = ('INI_SOL', 'WK_IRR')
    int_range = widgets.Dropdown(options=wave_options)
    Xc_WK = widgets.BoundedFloatText()
    DEP_WK = widgets.BoundedFloatText()
    
    display(int_range,Xc_WK,DEP_WK)
    
    def on_value_change(value):
        if int_range.value == 'WK_IRR':
            Xc_WK.description = 'Xc_WK'
            DEP_WK.description='DEP_WK'
    
        else:
            Xc_WK.description ='amp'
            DEP_WK.description='DEP'
    
    
    int_range.observe(on_value_change, 'value')
    

    【讨论】:

      猜你喜欢
      • 2021-08-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-13
      • 2016-07-01
      • 1970-01-01
      • 2019-11-05
      • 1970-01-01
      相关资源
      最近更新 更多