gpt4 book ai didi

jupyter-notebook - 观察 ipywidget slider 的方法不起作用

转载 作者:行者123 更新时间:2023-12-04 01:13:26 26 4
gpt4 key购买 nike

我正在尝试在 slider 随动画变化时调用函数。而且它不起作用,只有当我通过鼠标手动更改时它才会调用函数。这是我的代码:

import ipywidgets as widgets

play = widgets.Play(
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])

你能帮我管理一下吗?

最佳答案

从这个问题 (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])

关于jupyter-notebook - 观察 ipywidget slider 的方法不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54834289/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com