我有这个代码 ipython 代码:
import ipywidgets as widgets
from IPython.display import display
import time
w = widgets.Dropdown(
options=['Addition', 'Multiplication', 'Subtraction'],
value='Addition',
description='Task:',
)
def on_change(change):
print("changed to %s" % change['new'])
w.observe(on_change)
display(w)
它按预期工作。当小部件的值发生变化时,该on_change
函数被触发。但是,我想运行长时间的计算并定期检查小部件的更新。例如:
for i in range(100):
time.sleep(1)
# pull for changes to w here.
# if w.has_changed:
# print(w.value)
我怎样才能做到这一点?