bootstack.ContextMenu#
- class bootstack.ContextMenu(target=None, *, min_width=150, width=None, min_height=None, height=None, anchor='nw', attach='se', offset=None, hide_on_outside_click=True, trigger='right_click', on_select=None, items=None, density='default', parent=None)#
Bases:
objectA popup menu that attaches to a target widget and opens on a gesture or manual call.
The menu renders as a themed floating popup on Windows and Linux, and as a native system menu on macOS. The public API is identical across platforms.
- Parameters:
target (Any) – Widget the menu attaches to for positioning and auto-trigger binding. Accepts any bootstack widget. If omitted, call
show(position=(x, y))manually.min_width (int) – Minimum menu width in pixels. Default
150.width (int | None) – Fixed menu width in pixels. Defaults to auto (uses
min_width).min_height (int | None) – Minimum menu height in pixels. Defaults to auto.
height (int | None) – Fixed menu height in pixels. Defaults to auto-size to content.
anchor (Anchor) – Point on the menu aligned to
attachon the target. Default'nw'.attach (Anchor) – Point on the target the menu aligns to. Default
'se'.offset (tuple[int, int] | None) –
(dx, dy)pixel nudge applied after alignment.hide_on_outside_click (bool) – Hide the menu when the user clicks outside it. Default
True.trigger (Literal['right_click', 'left_click', 'double_click', 'manual'] | None) – Gesture that auto-shows the menu on
target.Nonemeans manual only — callshow()yourself. Default'right_click'.on_select (Callable[[MenuSelectEvent], Any] | None) – Callback fired whenever any item is activated. Called with a
MenuSelectEvent(type,text,valueof the activated item).items (list[Any] | None) – Initial items —
ContextMenuItemobjects or item dicts (each with atypekey plus item kwargs such astext,icon,on_click).density (WidgetDensity) – Item density. Default
'default'.parent (Any) – Parent widget for the Tk hierarchy. Defaults to
target.
- add_check_item(label, *, value=False, on_click=None, key=None)#
Add a checkbutton item.
- Parameters:
- Returns:
The key assigned to this item.
- Return type:
- add_item(label, *, on_click=None, icon=None, shortcut=None, disabled=False, key=None)#
Add a command item.
- Parameters:
label (str) – Item label text.
on_click (Callable[[], Any] | None) – Callback fired when the item is clicked.
icon (str | None) – Icon name shown beside the label.
shortcut (str | None) – Keyboard shortcut hint displayed on the right. Accepts a modifier pattern (
'Mod+S'→'Ctrl+S'/'⌘S'), a key registered with the Shortcuts service, or a literal string.disabled (bool) – If
True, item is visible but non-interactive.key (str | None) – Unique string identifier. Auto-generated if omitted.
- Returns:
The key assigned to this item.
- Return type:
- add_items(items)#
Add multiple items at once.
- Parameters:
items (list[ContextMenuItem | dict[str, Any]]) – List of
ContextMenuItemobjects or item dicts. Each dict has atypekey ('command','check','radio', or'separator') plus item kwargs such astext,icon,value, andon_click.
- add_radio_item(label, *, value=None, on_click=None, key=None)#
Add a radiobutton item.
All radio items in a menu share one group — selecting one deselects the others. Use check items if you need independent toggles.
- Parameters:
- Returns:
The key assigned to this item.
- Return type:
- add_separator(*, key=None)#
Add a horizontal separator.
- destroy()#
Destroy the menu and release all resources.
- hide()#
Hide the menu without destroying it.
- insert_item(index, type, *, on_click=None, **kwargs)#
Insert an item at a specific position.
- Parameters:
- Returns:
The key assigned to the new item.
- Return type:
- item(key)#
Return an item’s current configuration as a dict.
- Parameters:
key (str) – Key returned by an
add_*method.
- move_item(key, to_index)#
Move an existing item to a new position.