【发布时间】:2020-10-21 12:57:50
【问题描述】:
我需要一个将数组作为输入的小部件,例如一百个滑块(我不想手动创建)。
我怎样才能做到这一点?
我以为答案是widgets.Box,但后来我收到错误Box(...) cannot be transformed to a widget。
【问题讨论】:
标签: python matplotlib ipython ipywidgets matplotlib-widget
我需要一个将数组作为输入的小部件,例如一百个滑块(我不想手动创建)。
我怎样才能做到这一点?
我以为答案是widgets.Box,但后来我收到错误Box(...) cannot be transformed to a widget。
【问题讨论】:
标签: python matplotlib ipython ipywidgets matplotlib-widget
知道了——诀窍是使用**kwargs。我的代码看起来像这样。
seed = np.zeros(100)
kwargs = {'seed[{}]'.format(i) :
widgets.FloatSlider(
min = -1.0,
max = 1.0,
step = 0.01,
value = seed[i])
for i in range(100)}
@interact(**kwargs)
def Generate(**kwargs):
return seed
仍然会感谢更好的解决方案,因为我不知道如何使用此解决方案更改布局和内容。
【讨论】:
我认为您需要HBox 或VBox,而不仅仅是Box 用于容器。
import ipywidgets as widgets
from ipywidgets import interact
n = 10
seed = np.zeros(n)
sliders = list(widgets.FloatSlider(
description = 'seed[{}]'.format(i),
min = -1.0,
max = 1.0,
step = 0.01,
value = seed[i])
for i in range(n))
widgets.VBox(children = sliders)
【讨论】: