Menu
A menu — for a window's menu bar or a right-click pop-up. Build it by adding
entries, each of which may carry a command callback.
Example — a menu bar
const fileMenu = app.Menu({ tearOff: false });
fileMenu.addCommand({ label: 'New', command: () => newDoc() });
fileMenu.addCommand({ label: 'Open…', command: () => openDoc() });
fileMenu.addSeparator();
fileMenu.addCommand({ label: 'Quit', command: () => app.quit() });
const bar = app.Menu({ tearOff: false });
bar.addCascade({ label: 'File', menu: fileMenu });
app.menu = bar; // attach to the window
Example — a pop-up
const ctx = app.Menu({ tearOff: false });
ctx.addCommand({ label: 'Copy', command: copy });
// popup() posts with an input grab, so the menu dismisses on click-away / Esc.
widget.bind('<Button-3>', (e) => ctx.popup(e.screenX, e.screenY));
Options
| Option | Type | Description |
|---|---|---|
tearOff |
boolean | Whether the menu can be torn off — usually false. |
Methods
| Method | Description |
|---|---|
addCommand(opts) |
Add an item. opts: label, command, accelerator, state, … |
addCascade(opts) |
Add a submenu: { label, menu }. |
addCheckbutton(opts) / addRadiobutton(opts) |
Add a toggle / choice item (bind a variable). |
addSeparator() |
Add a divider. |
insert(index, type, opts?) |
Insert an entry at a position. |
entryConfig(index, opts) |
Reconfigure an entry. |
delete(index1, index2?) |
Remove entries. |
invoke(index) |
Activate an entry programmatically. |
popup(x, y) |
Show as a context menu at screen coords with an input grab — dismisses on click-away / Esc / selection. Prefer this for right-click menus. |
post(x, y) / unpost() |
Low-level show / hide at screen coords — no grab (stays up until unpost()); use popup() for context menus. |
count() |
Number of entries. |
Plus the inherited widget methods — see Widget basics.
See also
- The notes editor (menu bar) and file browser (context menu) examples
- Events & binding