0

I am trying to remove a child widget from an HBox ipython widget group within an ipython notebook. Creating the widget group looks like this:

    buttons = [widgets.Button(description=str(i)) for i in range(5)]
    mybox = widgets.HBox(children=buttons)
    mybox

This displays 5 buttons.

Now I have a group of five buttons and I would like to remove the last button. As far as I can tell the box object does not have a method to remove children. So my thought was to close the last widget in the group:

    mybox.children[-1].close()

Now, only the first 4 buttons are displayed (0, 1, 2, 3) which is what I want, however if I get the description from the group the 5th button is still there:

    [child.description for child in mybox.children]

    ['0', '1', '2', '3', '4']

The output I expected, and what I need is this:

   ['0', '1', '2', '3']

I can simply create a copy of a slice, however this causes other problems, and I would really like to be able to modify the original box.

This is not what I need:

    mybox = widgets.HBox(children=mybox.children[:-1])

1 Answer 1

1

The best answer I have been able to figure out after working on this is:

remove = mybox.children[-1]
mybox.children = mybox.children[:-1]
remove.close()

This is not perfect, but does work. Hopefully it will help anyone else with a similar problem.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.