【问题标题】:Remove ipython widget from box (previously container widget)从框中删除 ipython 小部件(以前是容器小部件)
【发布时间】:2015-08-07 17:22:51
【问题描述】:

我正在尝试从 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])

【问题讨论】:

    标签: widget ipython ipython-notebook hbox


    【解决方案1】:

    在解决这个问题后,我能想出的最佳答案是:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-17
      • 2011-07-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多