MultipleSelection and Interact from Ipywidgets not giving the desired display when defining a function

Viewed 17

I want to use the selected items of four different Multiple Select from ipywidgets. I can create the box with the display I want using this code:

box_layout = widgets.Layout(margin = '10px', border= '2px', padding = '10px', 
                            align_items = 'stretch',
                            justify_content = 'space-around',
                            width = '98%')

# Create the widgets
w1 = widgets.SelectMultiple(options = ["a","b","c"], rows=3)
w2 = widgets.SelectMultiple(options = ["d","e","f"], rows=3)
w3 = widgets.SelectMultiple(options = ["g","h","i"], rows=3)
w4 = widgets.SelectMultiple(options = ["j","k","m"], rows=3)

items = [
    widgets.VBox([widgets.Label(value = "ONE"), w1], layout = item_layout),
    widgets.VBox([widgets.Label(value = "TWO"), w2], layout = item_layout),
    widgets.VBox([widgets.Label(value = "THREE"), w3], layout = item_layout),
    widgets.VBox([widgets.Label(value = "FOUR"), w4], layout = item_layout)]

hbox = widgets.HBox(items, layout=box_layout)
display(hbox) 

I get this result (I like it, this is what I want): Image here

But then, when I try to use the selected items, with the chunk of code below, I don't get the desired result. Image here

@widgets.interact(one=w1, two=w2, three=w3, four=w4)
def trial(one, two, three, four):
    #do whatever e.g:
    display(hbox) 
    print(list(one+two+three+four))

One+two+three+four gives me the selected items (great!) but the display now is weird. Am I doing something wrong?

Thanks in advance! :)

0 Answers
Related