I am trying to learn how ipywidget dropdown menu works with an observe method and came across to this really useful SO question: ipywidgets dropdown widgets: what is the onchange event?
Good explanations can be found there.
Tweaking a bit some code of the answers I created this little script:
w = wd.Dropdown(
options=['Addition', 'Multiplication', 'Subtraction'],
value='Addition',
description='Task:',
)
def on_change(change):
print('method is called when printing this')
if change['type'] == 'change' and change['name'] == 'value':
print("changed to %s" % change.new)
else:
print('chage type is not change it is actually:', change['type'])
print('chage name is not value it is actually:', change['name'])
w.observe(on_change)
display(w)
The weird thing is that when changing the value of the dropdown menu ONE TIME this is what is printed out:
method is called when printing this
chage type is not change it is actually: change
chage name is not value it is actually: _property_lock
method is called when printing this
chage type is not change it is actually: change
chage name is not value it is actually: label
method is called when printing this
changed to Multiplication
method is called when printing this
chage type is not change it is actually: change
chage name is not value it is actually: index
method is called when printing this
chage type is not change it is actually: change
chage name is not value it is actually: _property_lock
So the observe method is called 4 times for one single change of the dropdown of the menu.
Why is that?
secondly this does not happened when the observe is written like this:
w.observe(on_change, names='value')
Then the output is just:
method is called when printing this
changed to Multiplication
So in the second case the method is called one once.
Can someone explain what is going on here?