widgets defined out of scope

You’re right that scope is the issue here, the variable names are created within the function scope and then lost when the function returns.

One solution would be to maintain a dictionary of widgets at the global level, and write into this dict whenever you change the number of desired widgets.

import ipywidgets as widgets
from IPython.display import display

widget_dict = {}

def return_widgets(size):
    return {str(x): widgets.IntText(description=str(x), value=x) for x in range(size)}

out = widgets.Output()
def on_value_change(change):
    out.clear_output()
    w = return_widgets(change['new'])
    for k,v in w.items():
        widget_dict[k] = v
    with out:
        display(*w.values())

size = widgets.IntText(value=3, min=1, max=5, description='size')

size.observe(on_value_change, 'value')
widgets.VBox([size, out])

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top