1

I have some fundamental question of the following article ipywidgets dropdown widgets: what is the onchange event?

Among the codes of the answer

def on_change(change):
    if change['type'] == 'change' and change['name'] == 'value':
        print("changed to %s" % change['new'])

what is

change['type']
change['name']
change['new']

Are these properties of a variable 'change'?
The values of these are strings?

Hope for complete information or at least key words for reference. Thanks!

sleeve chen
  • 221
  • 1
  • 11

1 Answers1

1

change is a variable that is created when the on_change (which is an event) is called (because of the user's interaction).

You wouldn't find the exact thing here but you will understand about it.

You can keep the variable change to anything you want to, for e.g, in the above link, there is one part like this -

from IPython.display import display
button = widgets.Button(description="Click Me!")
output = widgets.Output()

display(button, output)

def on_button_clicked(b):
    with output:
        print("Button clicked.")

button.on_click(on_button_clicked)

Here, b is also an event.

So, change is an event and - change['type'], change['name'], change['new'] can be used to access the property of the event.

So, for e.g- In the above code, you will see the description "Click Me", later on, if you want to print the description of the button which fired the event, you could do something like - print(b['description'])

So, basic idea is that it helps to access the property of the Interactive Widget.

PCM
  • 2,881
  • 2
  • 8
  • 30