我在Jupyter笔记本中创建小部件时遇到问题,该笔记本在更改其他小部件值时会更新。这是我一直在玩的代码:
from ipywidgets import interact, interactive, fixed
import ipywidgets as widgets
from IPython.display import display
def func(arg1,arg2):
print arg1
print arg2
choice = widgets.ToggleButtons(description='Choice:',options=['A','B'])
display(choice)
metric = widgets.Dropdown(options=['mercury','venus','earth'],description='Planets:')
text = widgets.Text(description='Text:')
a = interactive(func,
arg1=metric,
arg2=text,
__manual=True)
def update(*args):
if choice.value == 'A':
metric = widgets.Dropdown(options=['mercury','venus','earth'],description='Planets:')
text = widgets.Text(description='Text:')
a.children = (metric,text)
else:
metric = widgets.Dropdown(options=['monday','tuesday','wednesday'],description='Days:')
text2 = widgets.Textarea(description='Text2:')
a.children = (metric,text2)
choice.observe(update,'value')
display(a)
生成的小部件metric
和text
会根据是选择A
还是B
进行更改,但问题是“运行功能”按钮会立即消失当我改为B
时。我已尝试在__manual
之前添加display(a)
属性,在update
内添加{1}}以及其他几个地方。如何更改小部件框的子项而不覆盖我想手动运行该函数的事实?