How to add widgets to container widget in ipython/jupyter

泪湿孤枕 提交于 2020-01-05 06:33:28

问题


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

I try the following code

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)

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


回答1:


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)


来源:https://stackoverflow.com/questions/44986451/how-to-add-widgets-to-container-widget-in-ipython-jupyter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!