观察ipywidget滑块的方法不起作用

时间:2019-02-22 19:58:31

标签: jupyter-notebook ipywidgets

我尝试在滑块更改时调用函数,而该函数随动画而更改。而且它不起作用,仅当我用鼠标手动更改时它才调用函数。这是我的代码:

import ipywidgets as widgets

play = widgets.Play(
#     interval=10,
    value=0,
    min=0,
    max=100,
    step=1,
    description="Press play",
    disabled=False
)
slider = widgets.IntSlider(description="Slides")

def on_value_change(change):
    print(change['new'])

slider.observe(on_value_change, names='value') 
widgets.jslink((play, 'value'), (slider, 'value'))
widgets.HBox([play, slider])

可以帮我管理一下吗?

1 个答案:

答案 0 :(得分:1)

从本期(https://github.com/jupyter-widgets/ipywidgets/issues/1775)开始,该函数的输出似乎需要在输出小部件中捕获。参见Jason Grout的示例:

import ipywidgets as widgets

out = widgets.Output()
def on_value_change(change):
    with out:
        print(change['new'])

slider = widgets.IntSlider(min=1, max=100, step=1, continuous_update=True)
play = widgets.Play(min=1, interval=2000)

slider.observe(on_value_change, 'value')
widgets.jslink((play, 'value'), (slider, 'value'))
widgets.VBox([play, slider, out])