menubutton

Tk's menubutton documentation: http://www.tcl-lang.org/man/tcl9.0/TkCmd/menubutton.htm

See here for the themed version: ttk::menubutton


Example:

        frame .menubar -borderwidth 0 -takefocus 0
        pack .menubar -side top -fill x
        menubutton .menubar.file -text File \
            -underline 0 -menu .menubar.file.m 
        menubutton .menubar.edit -text Edit \
            -underline 0 -menu .menubar.edit.m
        menu .menubar.file.m -tearoff 0
        menu .menubar.edit.m -tearoff 0
        pack .menubar.file .menubar.edit -side left
        .menubar.file.m add command -label "Exit" \
            -underline 1 -command exit
        .menubar.edit.m add command -label "Cut" \
            -underline 2 \
            -command {event generate [focus] <<Cut>>}
        .menubar.edit.m add command -label "Copy" \
            -underline 0 \
            -command {event generate [focus] <<Copy>>}
        .menubar.edit.m add command -label "Paste" \
            -command {event generate [focus] <<Paste>>}
        text .t -wrap word
        pack .t -side top -fill both -expand y



What do menubuttons offer that menubars don't? Chengye Mao observes that a "menubutton works in an embedded window and a menubar does not." Moreover, some situations (including use of icons rather than labels?) preclude use of native menubars.

One disadvantage to using menubuttons rather than a native menubar is that on windows you don't get proper behavior when dragging from one menubutton to another. This is because the "unpost" menu command doesn't work on windows.

One advantage to using menubuttons rather than a native menubar is that you can have other widgets on the menubar (e.g. search box, status indicator, etc). Most UI design guidelines frown on such activities, however.

Bindings

HaO2026-10-27 The bindings may be found in file lib/tk/menu.tcl.

For example to also use the right button to post the menu:

menubutton $WiButton
bind $WiButton <ButtonPress-3> [bind Menubutton <ButtonPress-1>]
bind $WiButton <ButtonRelease-3> [bind Menubutton <ButtonRelease-1>]
bind $WiButton <B3-Motion> [bind Menubutton <B1-Motion>]