如何在ipython/jupyter中将小部件添加到容器小部件 [英] How to add widgets to container widget in ipython/jupyter

查看:110
本文介绍了如何在ipython/jupyter中将小部件添加到容器小部件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图制作VBox小部件,并在单击按钮时添加带有文本的新行.

I am trying to make VBox widget and and add a new row with text when button is clicked.

我尝试以下代码

import ipywidgets as wg
from ipywidgets import Layout
from IPython.display import display

vb = wg.VBox([wg.Text('1'),wg.Text('2')])
btn = wg.Button(description = 'Add') 

def on_bttn_clicked(b):        
    vb.children=tuple(list(vb.children).append(wg.Text('3'))) 

btn.on_click(on_bttn_clicked)
display(vb, btn)

list(hb.children)

但是分配"hb.children ="不起作用... 有没有一种方法可以在同一单元格中用代码编辑容器小部件?

But the assignment "hb.children=" does not work... Is there a way to edit container widgets with code in the same cell?

推荐答案

您可以使用简单的加号来连接两个列表.

You can use a simple plus to concatenate two lists.

vb.children=tuple(list(vb.children) + [new_button])

因此您的完整脚本将如下所示:

So your full script will look like this:

import ipywidgets as wg
from ipywidgets import Layout
from IPython.display import display

vb = wg.VBox([wg.Text('1'),wg.Text('2')])
btn = wg.Button(description = 'Add') 

def on_bttn_clicked(b):        
    vb.children=tuple(list(vb.children) + [wg.Text('3')]) 

btn.on_click(on_bttn_clicked)
display(vb, btn)

list(vb.children)

这篇关于如何在ipython/jupyter中将小部件添加到容器小部件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆