【问题标题】:read ipywidgets slider value in infinite loop在无限循环中读取 ipywidgets 滑块值
【发布时间】:2020-04-26 14:01:37
【问题描述】:

我试图在无限循环中读取滑块值,但没有打印输出。我的目标是用滑块控制机器人,即使滑块值没有变化,机器人也需要不断的信息流,因此是无限的while循环。

import ipywidgets as widgets
from IPython.display import display

slider = widgets.IntSlider()
display(slider)

while True:
    if slider.value != 0:
        print(slider.value)

【问题讨论】:

  • 您需要研究异步小部件的使用。当您的代码在 while 循环中时,不会发送对小部件的更新。 ipywidgets.readthedocs.io/en/latest/examples/…
  • 非常感谢,我设法使它与下面的库一起工作,在这种情况下异步对我不起作用。

标签: python-3.x jupyter-lab ipywidgets


【解决方案1】:

我设法使用这个库使它工作:https://pypi.org/project/jupyter-ui-poll/

global slider_result_value
with ui_events() as poll:    
    # this unfreezes the ui and allows slider event handlers to fire
    # allowing sliders to update global variables which can be read out
    poll(10) # poll for 10 ui events

    do_something(slider_result_value)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-06
    • 2019-05-25
    • 1970-01-01
    • 2019-11-25
    • 2015-04-07
    相关资源
    最近更新 更多