0

I have been trying to add items to my table using the selected item on combo box when I press the add button. Also the way I do it right now gives a faulty result that is being upside/down..

I tried multiple things but none worked. I can link the selected item from combo box to table and whenever I press to the "Add" button it links both but does not add.

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)

I want my table to be filled with the context inside combo box as the "Add" button is being pressed. Doing so, combo box's specific item should be removed from the combo box list and added to the table.

1 Answers1

0

I think you need something like this for your on_button_clicked function:

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)

Basically this edits options for Table2 to include the thing currently selected in Combo and edits options for Combo to exclude it.

gereleth
  • 2,452
  • 12
  • 21