1

It seems that there is no on_click option with dropdown widgets, I was wondering if there is some sort of workaround. One method I was thinking was, everytime an option is chosen, to flush the options and start the dropdown from the top again, where the top option would be the empty "".

For instance suppose I have:

from IPython.html import widgets
from IPython.display import display

def dropdown_event_handler(change):
    print(change.new)
    # flush the options and start from "" again

options = ["", "A", "B"]
dropdown = widgets.Dropdown(options=options, description="Categories")
dropdown.observe(dropdown_event_handler, names="value")
display(dropdown)

So the desired behaviour is that if I press "A" and "A" again, A would be printed out twice.

2
  • Buttons have an on_click method, but you don't click a Dropdown, you choose an option. Sounds like it would be better served by an array of Buttons for your different options? Commented Jul 28, 2020 at 7:25
  • I'm trying to build a text labeller. And the number of labels can be 100+ so probably not a good idea. Commented Jul 28, 2020 at 7:57

1 Answer 1

1

As you already suggested, you could set the value of the widget to "" after each change:

from IPython.html import widgets
from IPython.display import display

def dropdown_event_handler(change):
    print(change.new)
    dropdown.value = ""       

options = ["", "A", "B"]
dropdown = widgets.Dropdown(options=options, description="Categories")
dropdown.observe(dropdown_event_handler, names='value')

display(dropdown)

And I fear that is your only option. The Dropdown widget has no other type than "change". You can see all available types by printing them with type=All.

from IPython.html import widgets
from IPython.display import display
from traitlets import All

def dropdown_event_handler(change):
    print(change)    

options = ["", "A", "B"]
dropdown = widgets.Dropdown(options=options, description="Categories")
dropdown.observe(dropdown_event_handler, type=All)

display(dropdown)

Output:

{'name': '_property_lock', 'old': traitlets.Undefined, 'new': {'index': 1}, 'owner': Dropdown(description='Categories', options=('', 'A', 'B'), value=''), 'type': 'change'}
{'name': 'label', 'old': '', 'new': 'A', 'owner': Dropdown(description='Categories', index=1, options=('', 'A', 'B'), value=''), 'type': 'change'}
{'name': 'value', 'old': '', 'new': 'A', 'owner': Dropdown(description='Categories', index=1, options=('', 'A', 'B'), value='A'), 'type': 'change'}
{'name': 'index', 'old': 0, 'new': 1, 'owner': Dropdown(description='Categories', index=1, options=('', 'A', 'B'), value='A'), 'type': 'change'}
{'name': '_property_lock', 'old': {'index': 1}, 'new': {}, 'owner': Dropdown(description='Categories', index=1, options=('', 'A', 'B'), value='A'), 'type': 'change'}

So you can't observe a value in a Dropdown widget if it did not change. For more information see the Traitlets documentation.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.