0

我正在尝试从 ipython 笔记本中的 HBox ipython 小部件组中删除子小部件。创建小部件组如下所示:

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

这将显示 5 个按钮。

现在我有一组五个按钮,我想删除最后一个按钮。据我所知,盒子对象没有删除孩子的方法。所以我的想法是关闭组中的最后一个小部件:

    mybox.children[-1].close()

现在,只显示前 4 个按钮(0、1、2、3),这是我想要的,但是如果我从组中获得描述,第 5 个按钮仍然存在:

    [child.description for child in mybox.children]

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

我期望的输出,我需要的是:

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

我可以简单地创建一个切片的副本,但这会导致其他问题,我真的希望能够修改原始框。

不是我需要的:

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

1 回答 1

1

在解决这个问题后,我能够弄清楚的最佳答案是:

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

这并不完美,但确实有效。希望它能帮助其他有类似问题的人。

于 2015-08-07T19:19:13.730 回答