Ipywidgets: Discussion: Why is there a ToggleButtons?

Created on 26 Sep 2018  路  2Comments  路  Source: jupyter-widgets/ipywidgets

After reading this, and doing some work on the icons, I thought, why do we have a ToggleButtons widget?

The way Qt does this make more sense to me. Having a ToggleButtonGroup that manages the state of several ToggleButtons, with an exclusive (boolean) property will make the codebase much cleaner I think. Also, this allows one to control all the togglebuttons individually (style/layout).

Is there a good reason for this design decision, or is this maybe a good way forward?

Most helpful comment

I believe the question you mean to pose is "Why isn't there a ToggleButtons"

I'm new to widgets, but this is how I implemented the feature on my own:

import ipywidgets as widgets
toggle_buttons= {'a':widgets.ToggleButton(description='a', value=False),
          'b':widgets.ToggleButton(description='b', value=False),
          'c':widgets.ToggleButton(description='c', value=False),
          'd':widgets.ToggleButton(description='d', value=False)}

toggle_disp = widgets.VBox([
                widgets.HBox([toggle_buttons['a'], toggle_buttons['b']]),
                widgets.HBox([toggle_buttons['c'], toggle_buttons['d']])
                            ])

def on_toggle(**toggles):
    print(toggles)
    # do something with list of selected

interact_out = widgets.interactive_output(on_toggle, toggle_buttons)
display(toggle_disp, interact_out)

All 2 comments

I believe the question you mean to pose is "Why isn't there a ToggleButtons"

I'm new to widgets, but this is how I implemented the feature on my own:

import ipywidgets as widgets
toggle_buttons= {'a':widgets.ToggleButton(description='a', value=False),
          'b':widgets.ToggleButton(description='b', value=False),
          'c':widgets.ToggleButton(description='c', value=False),
          'd':widgets.ToggleButton(description='d', value=False)}

toggle_disp = widgets.VBox([
                widgets.HBox([toggle_buttons['a'], toggle_buttons['b']]),
                widgets.HBox([toggle_buttons['c'], toggle_buttons['d']])
                            ])

def on_toggle(**toggles):
    print(toggles)
    # do something with list of selected

interact_out = widgets.interactive_output(on_toggle, toggle_buttons)
display(toggle_disp, interact_out)

No that is not what I mean. There is a ToggleButtons, what you mean is a non-exclusive list of Toggle Buttons, which indeed you can create like you do in this example, so no need for a special widget.

Which again, raises the question of why we would have special widgets for special use cases, instead of 'atomic' building blocks.

Actually, I think ToggleButton should not even exist. Similar to Qt's QPushButton, Button could have a checkable trait (marking it as a toggle button) and inherit from _Bool.

The only downside I see of removing ToggleButtons is that there can be states where a group of buttons can be all be checked or more than one checked.

Was this page helpful?
0 / 5 - 0 ratings