I’m writing something where I sometimes need to programmatically change the value of widgets. Sample code:
# Update single widgets without triggering callbacks
widget.unobserve_all()
widget.value = new_value # doesn't work for dropdowns???
widget.index = widget.options.index(param_value) # works for dropdowns
widget.send_state() # irrelevant??
widget.observe(lambda change, n=param_name:
I expected widget.value
to successfully change the value of the widget - both internally and in the display. But only changing the index actually works. Does this mean there is a different way to programmatically change the value for every kind of widget? Is there a central way to do this that will work the same way for all widgets?
Thanks for any help!
Your code does not work at all so nobody knows what you are trying to achieve and where the widget module comes from.
I don’t use widgets much but the most basic example i can give you using ipywidgets
looks like this:
import ipywidgets
def i_am_function(name_me_pls):
return "Hello " + name_me_pls + "!"
ipywidgets.interact(i_am_function, name_me_pls="landoskape")
Other widgets can be invoked by changing the second paramer of the interact call.
A bool gives a checkbox and numbers a slider (start, end, stepsize).
2 Likes
I was being silly and didn’t realize that unobserve_all
will prevent the widget.value = new_value from updating the GUI. It’s more selective with what it unobserves and now it works.
1 Like
That’s an excellent example of a MWE. Apologies for not providing one. I figured out what was going wrong (unobserve_all also unobserves internal callbacks, so removing that line allows me to update the widget value). Thanks for your help.
1 Like