import panel as pn
pn.extension()
The MenuButton
widget allows specifying a list of menu items to select from triggering events when the button is clicked. Unlike other widgets, it does not have a value
parameter. Instead it has a clicked
parameter that can be watched to trigger events and which reports the last clicked menu item.
For more information about listening to widget events and laying out widgets refer to the widgets user guide. Alternatively you can learn how to build GUIs by declaring parameters independently of any specific widgets in the param user guide. To express interactivity entirely using Javascript without the need for a Python server take a look at the links user guide.
For layout and styling related parameters see the customization user guide.
clicked
(str): The last clicked menu itemitems
(list(tuple or str or None): Menu items in the dropdown. Allows strings, tuples of the form (title, value) or Nones to separate groups of items.split
(boolean): Whether to add separate dropdown area to button.button_type
(str): A button theme; should be one of 'default'
(white), 'primary'
(blue), 'success'
(green), 'info'
(yellow), or 'danger'
(red)disabled
(boolean): Whether the widget is editablename
(str): The title of the widgetThe MenuButton
is defined by the name of the button and a list of items corresponding to the menu items. By separating items by None we can group them into sections:
menu_items = [('Option A', 'a'), ('Option B', 'b'), ('Option C', 'c'), None, ('Help', 'help')]
menu_button = pn.widgets.MenuButton(name='Dropdown', items=menu_items, button_type='primary')
pn.Column(menu_button, height=200)
The clicked
parameter will report the last menu item that was clicked:
menu_button.clicked
The on_click
method can trigger function when button is clicked:
text = pn.widgets.TextInput(value='Ready')
def b(event):
text.value = f'Clicked menu item: "{event.new}"'
menu_button.on_click(b)
pn.Row(menu_button, text, height=300)
The color of the button can be set by selecting one of the available button types:
pn.Column(*(pn.widgets.MenuButton(name=p, button_type=p, items=menu_items) for p in pn.widgets.MenuButton.param.button_type.objects))
Additionally we can move the dropdown indicator into a separate area using the split option:
pn.Row(pn.widgets.MenuButton(name='Split Menu', split=True, items=menu_items), height=200)
If the button itself is clicked in split
mode, the clicked
property will report the value of the name
parameter, i.e. in this case clicking it will set the clicked
parameter to 'Split Menu'
.
The MenuButton
widget exposes a number of options which can be changed from both Python and Javascript. Try out the effect of these parameters interactively:
pn.Row(menu_button.controls, menu_button)