Skip to content

MenuBar

oldmangan edited this page Oct 7, 2026 · 1 revision

A File / Edit / View style application menu. Each title opens a ContextMenu dropdown; a title without items is a plain command on the bar. Added in #44.

from wapyt import MenuBarConfig, MenuItem

bar = self.add_menubar("mainwindow_header", MenuBarConfig(items=[
    MenuItem("file", "File", items=[
        MenuItem("new", "New", "mdi-file-outline", shortcut="Ctrl+N"),
        MenuItem("recent", "Open recent", items=[MenuItem("r1", "notes.md")]),
        MenuItem("save", "Save", "mdi-content-save-outline", disabled=True),
        MenuItem(separator=True),
        MenuItem("quit", "Quit", danger=True),
    ]),
    MenuItem("view", "View", items=[
        MenuItem("wrap", "Word wrap", checkable=True, checked=True),
        MenuItem(separator=True),
        MenuItem("light", "Light", group="theme", checked=True),
        MenuItem("dark", "Dark", group="theme"),
    ]),
    MenuItem("help", "Help"),                     # a plain command on the bar
], label="Main menu"))

bar.on_select(lambda p: run(p["id"], p.get("checked")))
bar.set_disabled("save", False)                   # e.g. once the document has changes

Layout.add_menubar(id="mainwindow_header", config) mounts it in a cell, or construct MenuBar(config, container=...) / root=....

Items

Top-level entries are MenuItems. One with items is a menu title; one without is a command. Inside the dropdowns everything a ContextMenu supports works: icons, shortcut hints, danger and disabled items, separators, submenus, and checked items:

  • MenuItem(checkable=True, checked=...): a tick that flips when chosen.
  • MenuItem(group="theme", checked=...): one of a radio group; choosing it unchecks the others.

Ids must be unique across the whole bar. The top level cannot hold separators.

Keyboard and mouse

  • One tab stop. ← / → / Home / End move between titles; ↓, Enter or Space opens a menu with its first item focused.
  • Inside a menu: ↑ / ↓ move, → opens a submenu, and ← / → at the top level move to the neighbouring menu. Escape closes it and returns to the title; Tab closes it.
  • Mouse: click a title to open it, click it again to close it. Once a menu is open, pointing at another title switches to it. Clicking outside closes it.
  • Disabled titles and items can be focused but not used; hidden ones are skipped.

Events and methods

Event Payload
on_select {id, menu}: menu is the title's id, or None for a plain command. Checkable and group items add checked (the new state)
on_open {id} of the title whose menu opened, e.g. to update what it enables first
Method
set_disabled(ids, disabled=True) Titles or items anywhere in the bar; False enables them, even ones declared disabled
set_hidden(ids, hidden=True) Hide or show titles or items
set_checked(id, checked=True) Without firing on_select
is_disabled, is_hidden, is_checked
open(id), close(), is_open()
set_items(items), destroy()

Labels are set as text, never markup.

Clone this wiki locally