【问题标题】:update of dropdown widget (ipywidget)下拉小部件的更新(ipywidget)
【发布时间】:2018-10-23 12:52:16
【问题描述】:

我正在使用交互式 ipywidget 下拉菜单来过滤一定数量的数据并用 plotly 绘制图形。

我的问题在于下拉小部件的更新:这是一个简化的示例:

    import ipywidgets as widgets
from ipywidgets import *
from IPython.display import display, clear_output, HTML

def update_drop2(*args):
    if Drop1.value=='R':
        Drop2.options = {'un':'1','trois':'3','quatre':'4'}
    if Drop1.value=='B':
        Drop2.options = {'un':'1','trois':'3','quatre':'4'}
    if Drop1.value=='G':
        Drop2.options = {'deux':'2','quatre':'4','trois':'3'}

def action(*args):
    print('action')


Drop1 = widgets.Dropdown(
        options = {'red':'R','blue':'B','green':'G'},
        value = 'R',
        description='Drop1 :',
        disabled=False,
        continuous_update=False,
        style = {'description_width': 'initial'}
        )

Drop2 = widgets.Dropdown(
        options = {'un':'1','deux':'2','trois':'3'},
        value = '3',
        description='Drop2 :',
        disabled=False,
        continuous_update=False,
        style = {'description_width': 'initial'}
        )

display(Drop1,Drop2)

Drop1.observe(update_drop2,'value')
Drop2.observe(action,'value')

drop1 改变了 drop2 的选项。 drop1 的任何更改(并且 drop2 应该启动一个操作(这里只是一个示例)

  • 当 drop2 的选项列表不同并由 drop1 更改时,drop2.value 会自动设置为新列表的第一个值,即使选择的值已存在于前一个列表中。如果它仍然在新列表中可用,是否有保留此值的选项? (例如:选择 drop1 = R, drop2 = 'trois 然后做 drop1='G' :drop2 自动设置在'2'。选择 drop1 = R, drop2 = 3 然后做 drop1='B' :drop2 保留在'3')

  • 如果我在 drop2.options 更新后尝试强制 drop2.value,这会使 drop2 = 2 动作发生 2 次更改... ,有时我有重复的动作,有时我没有动作......

感谢您的帮助

【问题讨论】:

    标签: python jupyter-notebook ipywidgets


    【解决方案1】:

    下面的代码应该可以解决你的两个问题:

    import ipywidgets as widgets
    from ipywidgets import *
    from IPython.display import display, clear_output, HTML
    
    out = widgets.Output()
    
    def action(*args):
        with out:
            print('action')
            
    
    def update_drop2(*args):
            temp = Drop2.value
            Drop2.unobserve(action, 'value')
            
            if Drop1.value=='R':
                Drop2.options = {'un':'1','trois':'3','quatre':'4'}
            if Drop1.value=='B':
                Drop2.options = {'un':'1','trois':'3','quatre':'4'}
            if Drop1.value=='G':
                Drop2.options = {'deux':'2','quatre':'4','trois':'3'}
            
            if str(temp) in Drop2.options.values():
                Drop2.value = temp
            
            Drop2.observe(action, 'value')
            action()
    
    Drop1 = widgets.Dropdown(
            options = {'red':'R','blue':'B','green':'G'},
            value = 'R',
            description='Drop1 :',
            disabled=False,
            continuous_update=False,
            style = {'description_width': 'initial'}
            )
    
    Drop2 = widgets.Dropdown(
            options = {'un':'1','deux':'2','trois':'3'},
            value = '3',
            description='Drop2 :',
            disabled=False,
            continuous_update=False,
            style = {'description_width': 'initial'}
            )
    
    display(Drop1, Drop2, out)
    
    Drop1.observe(update_drop2,'value')
    Drop2.observe(action, 'value')
    
    1. 我添加了Output 小部件来实际显示输出。

    2. 要修复您的第一个要点,可以使用临时变量temp 来检查Drop2 的值是否包含在Drop2.options 的字典中,并在需要时相应地更改值。 Drop2.value 发生了变化,因为字典是从 python 3.7 开始排序的。

    3. 要解决您的第二个要点,即在更改Drop1.value 时不进行双重调用,这也可能会更改Drop2.valueDrop2.options,可以在函数Drop2update_drop2 中不观察Drop2最后再观察一下。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-12-12
      • 2020-04-21
      • 1970-01-01
      • 2011-03-06
      • 1970-01-01
      • 1970-01-01
      • 2020-07-25
      • 1970-01-01
      相关资源
      最近更新 更多