我正在尝试从ipython笔记本中的HBox ipython小部件组中删除子小部件。创建窗口小部件组如下所示:
buttons = [widgets.Button(description=str(i)) for i in range(5)]
mybox = widgets.HBox(children=buttons)
mybox
这会显示5个按钮。
现在我有一组五个按钮,我想删除最后一个按钮。据我所知,box对象没有删除子进程的方法。所以我的想法是关闭组中的最后一个小部件:
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])
答案 0 :(得分:0)
我在解决这个问题后能够找到的最佳答案是:
remove = mybox.children[-1]
mybox.children = mybox.children[:-1]
remove.close()
这不完美,但确实有效。希望它可以帮助其他有类似问题的人。