【问题标题】:How do I add selected items from combo box to a table in Jupyter widgets?如何将组合框中的选定项目添加到 Jupyter 小部件的表格中?
【发布时间】:2019-08-18 14:40:33
【问题描述】:

当我按下添加按钮时,我一直在尝试使用组合框中的选定项目将项目添加到我的表中。我现在这样做的方式也给出了一个错误的结果,即颠倒/倒置..

我尝试了多种方法,但都没有奏效。我可以将组合框中的选定项目链接到表格,每当我按下“添加”按钮时,它都会链接两者但不添加。

from ipywidgets import Layout, interact, interact_manual, fixed
import ipywidgets as widgets
from IPython.display import display
import matplotlib.pyplot as plt
import numpy as np
Arr=['Weight','Power', '10s-Peak Power','Volume']

Combo=widgets.Dropdown(options=[Arr[0],Arr[1],Arr[2],Arr[3]],description='Requirement')

Table2=widgets.SelectMultiple(index=(0,), options = [''], rows=10,description='Desired Requirements',
    disabled=False,layout=Layout(width ='450px'))

button = widgets.Button(description="Add",icon='check')

output = widgets.Output()

def on_button_clicked(b):
    with output:
        d=widgets.link((Combo,'value'),(Table2,'options'))
        d.unlink()

button.on_click(on_button_clicked)

display(button,Combo,Table2)

我希望我的表格在按下“添加”按钮时填充组合框中的上下文。这样做,组合框的特定项目应从组合框列表中删除并添加到表中。

【问题讨论】:

    标签: python jupyter-notebook widget ipywidgets


    【解决方案1】:

    我认为您的on_button_clicked 功能需要这样的东西:

    def on_button_clicked(b):
        new_option = Combo.value
        Table2.options = (*Table2.options, new_option)
        Combo.options = (o for o in Combo.options if o!=new_option)
    

    基本上,这会编辑Table2 的选项以包含当前在Combo 中选择的事物,并编辑Combo 的选项以排除它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-25
      • 1970-01-01
      • 1970-01-01
      • 2018-03-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多