Skip to content

Toolbar

oldmangan edited this page Oct 8, 2026 · 3 revisions

A row of buttons, toggles, one-of-several groups, text, separators and spacers. When it runs out of room, buttons drop to icons.

from wapyt import ToolbarButton, ToolbarConfig, ToolbarSeparator, ToolbarSpacer, ToolbarText

toolbar = self.add_toolbar("mainwindow_header", ToolbarConfig(items=[
    ToolbarButton("new", "New connection", "mdi-plus"),
    ToolbarButton("delete", "Delete", "mdi-delete", variant="danger"),
    ToolbarSeparator(),
    ToolbarButton("tabbed", "Tabs", "mdi-table-row", group="layout", active=True, show_label=False),
    ToolbarButton("tiled", "Tiles", "mdi-view-grid", group="layout", show_label=False),
    ToolbarSpacer(),
    ToolbarButton("update", "Update", "mdi-arrow-up-circle", variant="accent", hidden=True),
    ToolbarText("user"),
    ToolbarButton("logout", "Logout", "mdi-logout"),
]))
toolbar.on_click(lambda p: self.on_toolbar(p["id"]))
toolbar.set_text("user", me["username"])

Layout.add_toolbar(id, toolbar_config) mounts it; id defaults to MainWindow's mainwindow_header cell. Elsewhere, use Toolbar(cfg, container=...).

Items

Item
ToolbarButton(id, label, icon, ...) A button (options below)
ToolbarText(id, text, hidden) Plain text, such as the signed-in user's name
ToolbarSeparator() A thin divider
ToolbarSpacer() Takes the free space, pushing what follows to the right

Ids must be unique within a toolbar.

Buttons

ToolbarButton
label, icon, tooltip Text, MDI class, hover text (defaults to the label); shown as a styled tooltip, also on keyboard focus
variant "default", "primary", "accent" (a highlighted call to action, such as an update notice) or "danger"
toggle=True Stays pressed until clicked again
group="..." One of several: pressing one releases the rest of its group
active Initial pressed state for toggles and groups
badge A small count or tag; 0 is shown
disabled, hidden Initial state
show_label=False Icon-only; the label becomes the tooltip and accessible name
keep_label=True Keep the label in compact mode

Dropdown and split buttons

Added in #50.

from wapyt import MenuItem

ToolbarButton("export", "Export", "mdi-export", items=[
    MenuItem("csv", "CSV"),
    MenuItem("json", "JSON"),
    MenuItem("more", "More formats", items=[MenuItem("xml", "XML")]),
])
ToolbarButton("run", "Run", "mdi-play", split=True, items=[
    MenuItem("run_debug", "Run with debugger", "mdi-bug", shortcut="F5"),
    MenuItem("run_profile", "Profile"),
])
  • items makes a dropdown: the button opens a menu under it instead of firing on_click. Menu entries are ContextMenu MenuItems, so submenus, separators, shortcut hints, danger, checkable and radio group items all work.
  • split=True keeps the button a normal command and adds a joined arrow beside it that opens the menu.
  • A pick fires on_select with {id, menu}, where menu is the button's id, plus checked for checkable and group items. on_open fires with {id} as a menu opens, in time to update it.
  • Ids are unique across the whole toolbar, buttons and menu items together, so set_disabled, set_hidden, is_disabled and is_hidden work on menu items too. set_checked(id) / is_checked(id) for checkable and group items; set_menu_items(id, items) replaces a menu (recent files, say; an empty menu doesn't open); open_menu(id), close_menu(), is_menu_open(id=None).
  • A dropdown can't also be a toggle or group button; a split one can, since its main part is still an ordinary button.

Events

on_click → {id, group, active}. active is the new pressed state for toggle and group buttons, otherwise None; group is None outside a group.

Changing it at runtime

Method
set_hidden(id, hidden=True) Show or hide, e.g. a "Reconnect all" button that only matters sometimes
set_text(id, text) Relabel a button ("Reconnect 3") or change a text item
set_badge(id, badge) None or "" removes it
set_disabled, set_icon, set_tooltip
set_active(id, active=True), is_active(id), get_active(group) Pressed state
set_items(items) Replace everything

All text is set as text, never markup.

Compact mode

ToolbarConfig(compact="auto") (the default) watches the toolbar's width. Once the labelled toolbar no longer fits, buttons with an icon drop their labels. They come back when there's room for the labelled width again. Labels remain the tooltip and accessible name. compact="always" or "never" force it.

Keyboard

Following the WAI-ARIA toolbar pattern, the toolbar is one Tab stop. ←/→ move between buttons and wrap, Home and End jump to the ends, and Enter or Space press. Toggles and group buttons expose aria-pressed.

On a dropdown (or a split button's arrow), Enter, Space or ↓ open the menu on its first item and ↑ on its last. In the menu, Escape closes it and returns to the button, and ←/→ close it and move along the toolbar, opening the next dropdown if that is one.

Clone this wiki locally