Menus
Live: the Menus card of the gallery.
menu, menubutton, and ttk::menubutton
menu .bar
. configure -menu .bar
foreach {name text} {file File edit Edit view View} {
menu .bar.$name -tearoff 0
.bar add cascade -label $text -menu .bar.$name -underline 0
}
.bar.file add command -label New -accelerator Ctrl+N -command {set status New}
.bar.file add command -label Open... -accelerator Ctrl+O -command {set status Open}
.bar.file add separator
.bar.file add command -label Close -accelerator Ctrl+W -command {set status Close}
.bar.edit add command -label Undo -accelerator Ctrl+Z -command {set status Undo}
.bar.edit add command -label Redo -state disabled
.bar.view add checkbutton -label "Show grid" -variable grid
.bar.view add radiobutton -label Small -variable size -value small
.bar.view add radiobutton -label Large -variable size -value large
set grid 1
set size small
set status "Click File, press F10, or right-click"
ttk::menubutton .mb -text Size -menu .bar.view
label .status -textvariable status
pack .mb -pady {24 8}
pack .status
bind . <Button-3> {tk_popup .bar.edit %X %Y}

The details of menu, menubutton, and ttk::menubutton
menuhas the five entry types of Tk 8.6 (command,cascade,checkbutton,radiobutton, andseparator), the tearoff entry, and the subcommands and the messages ofwish8.6.16.invokesets the variable of a check or radio entry and then runs-command. A change of the active entry sends<<MenuSelect>>.toplevel -menugives the window a menu bar. On Linux and the web, ztk draws the bar as a row at the top of the window, as Tk on X11 does. The content of the window goes below the row. macOS and Windows show their own menus.tk_popupandpostshow a menu at a point of the window. A cascade opens on the right of its entry. On Wayland, each posted menu and each cascade is a popup surface (xdg_popup) that can extend past the window: the compositor flips a cascade to the left and slides a menu when the screen has no room. On the web, each one is a canvas in a layer above the page, and the viewport is the screen.menubuttonandttk::menubuttonpost their-menuon the side of-direction. A drag from the button to an entry and a release there invokes the entry.- The pointer: motion activates an entry and opens a cascade, a release invokes the entry, and a press outside the menus closes them.
- The keys:
UpandDownmove the active entry,Rightopens a cascade,Leftcloses it, andReturnorspaceinvokes the entry.Escapecloses the menu. The letter of-underlineinvokes its entry. On the menu bar,F10opens the first menu,Altwith a letter opens the menu with that-underlineletter, andLeftandRightmove along the bar. - A menu opens with a fade and a slide of 6 logical pixels in 150 ms, and fades out in 150 ms after it closes. The moves run on the render thread. The highlight of the active entry eases from one entry to the next in 120 ms.
- The parts
::item,::item-active,::separator,::accelerator, and::indicatorof the classMenuaccept CSS rules, and so do::bar,::bar-item, and::bar-item-activefor the menu bar row.Menubutton::indicatoris the chevron of a menu button.