menu(n)
π menu(n) β Tk Built-In Commands π
okay bestie, buckle up, because this page is about menu and tk_menuSetFocus β the whole create and manipulate 'menu' widgets and menubars situation. yes. menubars. the little strip at the top. we're doing this. π«Ά
β¨ NAME β¨
menu, tk_menuSetFocus β create and manipulate menu widgets and menubars, no cap. π
β¨ SYNOPSIS β¨
menu pathName ?options?
tk_menuSetFocus pathName
β¨ STANDARD OPTIONS β¨
bestie there are TWELVE of these and they're all iconic in their own way:
-activebackground-activeborderwidth-activeforeground-background-borderwidth-cursor-disabledforeground-font-foreground-relief-takefocus-activerelief
See the options manual entry for the details on the standard options β that's where the lore lives, iykyk. π
β¨ WIDGET-SPECIFIC OPTIONS β¨
π
-postcommand
- Command-Line Name:
-postcommand - Database Name:
postCommand - Database Class:
Command
so if you set this one, it gives you a Tcl command that gets executed every single time the menu is posted. the command is invoked by the post widget command before posting the menu. π
β οΈ heads up: in Tk 8.0 on Macintosh and Windows, all post-commands in a system of menus are executed before any of those menus are posted. that's literally just because of limitations in the individual platforms' menu managers. not us, not our fault, blame the platform. π
π¦ -selectcolor
- Command-Line Name:
-selectcolor - Database Name:
selectColor - Database Class:
Background
for menu entries that are check buttons or radio buttons, this option sets the color displayed in the indicator when the check button or radio button is selected. slay. πΈ
π -tearoff
- Command-Line Name:
-tearoff - Database Name:
tearOff - Database Class:
TearOff
this one has to have a proper boolean value (and the default is false, fyi), and it says whether or not the menu should have a tear-off entry at the top. β¨
if it does, that entry exists as entry 0 of the menu and all the other entries start numbering at 1. the default menu bindings arrange for the menu to be torn off when the tear-off entry is invoked. and this option is ignored under Aqua/macOS, where menus cannot be torn off. periodt. π
π§ -tearoffcommand
- Command-Line Name:
-tearoffcommand - Database Name:
tearOffCommand - Database Class:
TearOffCommand
if this option has a non-empty value, it's a Tcl command to invoke whenever the menu is torn off. and the actual command it runs is the value of this option, then a space, then the name of the menu window, then a space, then the name of the name of the torn off menu window. π«Ά
like, example time: if the option's value is a b and menu .x.y is torn off to create a new menu .x.tearoff1, then the command a b .x.y .x.tearoff1 gets invoked. see how it all comes together? iconic.
again β this option is ignored under Aqua/macOS, because menus cannot be torn off there. π
π -title
- Command-Line Name:
-title - Database Name:
title - Database Class:
Title
this string titles the window that gets created when this menu is torn off. if the title is NULL, then the window just uses the title of the menubutton, or the text of the cascade item, from whichever one invoked this menu. π
β -type
- Command-Line Name:
-type - Database Name:
type - Database Class:
Type
this one can be menubar, tearoff, or normal, and it's set when the menu is created. here's the tea though β while the string returned by the configuration database will change if you change this option, that does not affect the menu widget's behavior. it's used by the cloning mechanism and it's not normally set outside of the Tk library. so like... don't touch. π
β¨ INTRODUCTION β¨
okay so. the menu command creates a new top-level window (the one given by the pathName argument) and makes it into a menu widget. π
that menu widget can be used either as a pop-up window, or applied to a toplevel (with its -menu option) to make it into the menubar for that toplevel. extra options, the ones up above, can be specified on the command line or in the option database to configure aspects of the menu like its colors and font. menu returns its pathName argument. and at the moment you invoke this command, there must not already exist a window named pathName β but pathName's parent must exist. rules are rules bestie. β¨
a menu is a widget that displays a collection of one-line entries arranged in one or more columns. there are several different types of entries, each with different properties, and you can absolutely mix different types in a single menu. π
β οΈ important: menu entries are not the same as entry widgets. in fact menu entries are not even distinct widgets β the entire menu is one widget. one. single. widget. the way that whole thing is literally one widget?? π€―
menu entries display with up to three separate fields:
- the main field is a label β a text string, a bitmap, or an image β controlled by the
-label,-bitmap, and-imageoptions for the entry. πΈ - if the
-acceleratoroption is set for an entry, then a second textual field shows up to the right of the label. the accelerator typically describes a keystroke sequence that may be used in the application to cause the same result as invoking the menu entry. π and β big note β this is a display option, it does not actually set the corresponding binding (you do that with thebindcommand, bestie). - the third field is an indicator. it's only present for checkbutton or radiobutton entries. it shows whether the entry is selected or not, and it's displayed to the left of the entry's string. β
in normal use, an entry becomes active (and displays itself differently) whenever the mouse pointer is over the entry. if a mouse button is released over the entry, then the entry is invoked. and the effect of invocation is different for each type of entry β those effects are described below in the sections on individual entries. π¦
entries may be disabled, which makes their labels and accelerators display in dimmer colors. the default menu bindings will not allow a disabled entry to be activated or invoked. π but good news: disabled entries may be re-enabled, and at that point it becomes possible to activate and invoke them again. slay.
whenever a menu's active entry changes, a <<MenuSelect>> virtual event is send to the menu. then the active item can be queried from the menu, and you can take an action, like setting context-sensitive help text for the entry. π₯
β¨ TYPES OF ENTRIES β¨
π COMMAND ENTRIES
the most common kind of menu entry! a command entry behaves a lot like a button widget. when a command entry is invoked, a Tcl command gets executed. that Tcl command is specified with the -command option. simple, clean, iconic. π
π SEPARATOR ENTRIES
a separator is an entry displayed as a horizontal dividing line. a separator may not be activated or invoked, and it has no behavior other than its display appearance. it's literally just there to look pretty. main character energy, zero plot. π
πΈ CHECKBUTTON ENTRIES
a checkbutton menu entry behaves a lot like a checkbutton widget. when it's invoked it toggles back and forth between the selected and deselected states. π
when the entry is selected, a particular value gets stored in a particular global variable (as determined by the -onvalue and -variable options for the entry); when it's deselected, another value (determined by the -offvalue option) gets stored in that global variable.
an indicator box is displayed to the left of the label in a checkbutton entry. if the entry is selected, the indicator's center is displayed in the color given by the -selectcolor option for the entry; otherwise the indicator's center is displayed in the background color for the menu. β¨
and if a -command option is specified for a checkbutton entry, its value is evaluated as a Tcl command each time the entry is invoked β this happens after toggling the entry's selected state. order matters bestie. π«Ά
π¦ RADIOBUTTON ENTRIES
a radiobutton menu entry behaves a lot like a radiobutton widget. radiobutton entries are organized in groups, and only one entry in a group may be selected at a time. π
whenever a particular entry becomes selected, it stores a particular value into a particular global variable (as determined by the -value and -variable options for the entry). this action causes any previously-selected entry in the same group to deselect itself. and once an entry has become selected, any change to the entry's associated variable will cause the entry to deselect itself. π
grouping of radiobutton entries is determined by their associated variables: if two entries have the same associated variable, then they're in the same group. iykyk.
an indicator diamond is displayed to the left of the label in each radiobutton entry. if the entry is selected, the indicator's center is displayed in the color given by the -selectcolor option for the entry; otherwise the indicator's center is displayed in the background color for the menu. β
and if a -command option is specified for a radiobutton entry, its value is evaluated as a Tcl command each time the entry is invoked β this happens after selecting the entry. π
π§ CASCADE ENTRIES
a cascade entry is one with an associated menu (determined by the -menu option). cascade entries are how you build cascading menus, the little submenus that slide out. the postcascade widget command can be used to post and unpost the associated menu just next to of the cascade entry. π
β οΈ the associated menu must be a child of the menu containing the cascade entry β this is needed in order for menu traversal to work correctly. don't fight it.
a cascade entry posts its associated menu by invoking a Tcl command of the form
menu post x y
where menu is the path name of the associated menu, and x and y are the root-window coordinates of the upper-right corner of the cascade entry. π
on Unix, the lower-level menu is unposted by executing a Tcl command with the form
menu unpost
where menu is the name of the associated menu. on other platforms, the platform's native code takes care of unposting the menu. π
and if a -command option is specified for a cascade entry, then it's evaluated as a Tcl command whenever the entry is invoked. this is not supported on Windows though. π
β TEAR-OFF ENTRIES
a tear-off entry appears at the top of the menu if it's enabled with the -tearoff option. it is not like other menu entries in that it cannot be created with the add widget command and cannot be deleted with the delete widget command. special little guy. π
when a tear-off entry is created it appears as a dashed line at the top of the menu. under the default bindings, invoking the tear-off entry causes a torn-off copy to be made of the menu and all of its submenus. iconic behavior honestly. π₯
π MENUBARS π
Okay so bestie, literally any menu can be set as a menubar for a toplevel window (peep the toplevel command for syntax). On the Macintosh, whenever the toplevel is in front, this menu's cascade items will appear in the menubar across the top of the main screen π
. On Windows and Unix, this menu's items will be displayed in a menubar across the top of the window. These menus will behave according to the interface guidelines of their platforms β periodt.
In particular, since macOS requires that a menubar always be displayed for the active app, if no menu is assigned by a configure -menu for a certain toplevel then a standard default menu will be displayed whenever that toplevel has focus. Yes, macOS is that friend who insists on always having a menu bar visible π.
For every menu set as a menubar, a clone menu is made. See the CLONES section for more information π«Ά.
As noted, menubars may behave differently on different platforms. One example concerns the handling of checkbuttons and radiobuttons within the menu. While it is permitted to put these menu elements on menubars, they may not be drawn with indicators on some platforms, due to system restrictions β not Tk being shady, it's the system π.
β¨ SPECIAL MENUS IN MENUBARS β¨
Certain menus in a menubar will be treated specially. On the Macintosh, access to the special Application, Window and Help menus is provided. On Windows, access to the Windows System menu in each window is provided. On X Windows, a special right-justified help menu may be provided if Motif menu compatibility is enabled. In all cases, these menus must be created with the command name of the menubar menu concatenated with the special name. So for a menubar named .menubar, on the Macintosh, the special menus would be .menubar.apple, .menubar.window and .menubar.help; on Windows, the special menu would be .menubar.system; on X Windows, the help menu would be .menubar.help. Slay, naming convention π.
When Tk sees a .menubar.apple menu as the first menu in a menubar on the Macintosh, that menu's contents make up the first items of the Application menu whenever the window containing the menubar is in front. After all of the Tk-defined items, the menu will have a separator, followed by all standard Application menu items.
Technical caveat, bestie: such a.applemenu must be present in a menu when that menu is first configured as a toplevel's menubar, otherwise a default application menu (hidden from Tk) will be inserted into the menubar at that time and subsequent addition of a.applemenu will no longer result in it becoming the Application menu. No take-backsies π.
When Tk sees a .menubar.window menu on the Macintosh, the menu's contents are inserted into the standard Window menu of the user's menubar whenever the window's menubar is in front. The first items in the menu are provided by macOS, and the names of the current toplevels are automatically appended after all the Tk-defined items and a separator. The Window menu on the Mac also allows toggling the window into a fullscreen state, and managing a tabbed window interface (multiple windows grouped into a single window) if supported by that version of the operating system. Multi-window glamour β¨.
When Tk sees a .menubar.help menu on the Macintosh, the menu's contents are appended to the standard Help menu of the user's menubar whenever the window's menubar is in front. The first items in the menu are provided by macOS. Helpful icon behavior π.
When Tk sees a System menu on Windows, its items are appended to the system menu that the menubar is attached to. This menu is tied to the application icon and can be invoked with the mouse or by typing Alt+Spacebar. Due to limitations in the Windows API, any font changes, colors, images, bitmaps, or tearoff images will not appear in the system menu. Windows said "no aesthetic upgrades for you" π.
When Tk sees a Help menu on X Windows and Motif menu compatibility is enabled the menu is moved to be last in the menubar and is right justified. Motif menu compatibility is enabled by setting the Tk option *Menu.useMotifHelp to true. X Windows really said "help goes last, bestie" π
.
π¦ CLONES π¦
When a menu is set as a menubar for a toplevel window, or when a menu is torn off, a clone of the menu is made. This clone is a menu widget in its own right, but it is a child of the original. Changes in the configuration of the original are reflected in the clone. Additionally, any cascades that are pointed to are also cloned so that menu traversal will work right. Clones are destroyed when either the tearoff or menubar goes away, or when the original menu is destroyed. Clone behavior is giving "twins, but make it menu widget" π―ββοΈβ¨.
π WIDGET COMMAND π
okay so bestie, menu literally makes a brand-new Tcl command, and it's named pathName π. you use that command to do everything to the widget. here's the whole vibe:
pathName option ?arg ...?
option plus the args decide exactly what the command does. no notes. β¨
π indexes β how we point at stuff π
ngl, a lot of the menu widget commands take one argument that says which entry of the menu you're operating on. those little pointers are called indexes, and you can write them in any of these forms:
activeβ the entry that's active right now. if literally nothing is active, this is the same as{}. and no, you can't abbreviate this one. π ββοΈendβ the bottom-most entry in the menu. if the menu has zero entries, this is equivalent to{}. also can't be abbreviated, sorry babe.lastβ same asend. twin energy. π―{}β "no entry at all" bestie. most commonly used with theactivateoption to deactivate every entry in the menu. in most cases, specifying{}means nothing happens in the widget command at all. it's giving "ignore me". π@x,yβ the entry covering the point in the menu's window atxandy(that's pixel coordinates, okurrr). if no entry covers that point, it's equivalent to{}. and if you only give one number, it gets treated as the y-coordinate. π―numberβ pick the entry numerically, where0is the top-most entry,1is the one below it, and so on down. easy. βidβ if your index doesn't match any of the forms above, the menu gets searched for an entry with that specific id. π΅οΈββοΈpatternβ if literally all the other methods fail, we use this one. the pattern gets matched against the label of each entry, in order from the top down, until a matching entry is found. the rules ofstring matchapply. πΈ
and if an index could match more than one of these forms? the form that shows up earlier in the list above wins. periodt. also, indexes support the same simple interpretation as for the command string index, with simple integer index arithmetic and indexing relative to end. iykyk. π
π οΈ the widget commands π οΈ
these are all the commands you can throw at a menu widget, bestie:
pathName activate indexβ flips the entry atindexto active and redisplays it using its active colors. any entry that was active before gets deactivated. and ifindexis{}, or the entry you named is disabled, the menu ends up with no active entry at all. returns an empty string. πpathName add type ?id? ?option value option value ...?β adds a brand-new entry to the bottom of the menu. the new entry'stypehas to be one ofcascade,checkbutton,command,radiobutton, orseparatorβ or a unique abbreviation of one of those. if you pass theidargument, it's used as the entry identifier, andidmust not already exist in the menu. otherwise a new unique identifier gets generated for you. π any extra arguments specify the options listed in the MENU ENTRY OPTIONS section below. returns the id of the new entry, iconic.pathName cget optionβ returns the current value of the configuration option given byoption.optioncan be any of the values accepted by themenucommand. β¨pathName clone newPathname ?cloneType?β makes a clone of the current menu, namednewPathName. it's a menu in its own right, but every change to the clone gets propagated to the original menu and vice versa.cloneTypecan benormal,menubar, ortearoff. should not normally be called outside of the Tk library. see the CLONES section for more info. π§pathName configure ?option? ?value option value ...?β query or modify the configuration options of the widget. if you pass nooption, it returns a list describing all the available options forpathName(checkTk_ConfigureInfofor how that list is formatted). if you passoptionwith no value, you get a list describing that one named option (identical to the corresponding sublist from the no-option case). and if you pass one or more option-value pairs, it modifies those widget option(s) to have the given value(s) β in which case it returns an empty string.optionmay have any of the values accepted by themenucommand. πpathName delete index1 ?index2?β deletes every menu entry betweenindex1andindex2, inclusive. ifindex2is omitted it defaults toindex1. attempts to delete a tear-off menu entry are just ignored β instead, you should change the-tearoffoption to remove the tear-off entry. πpathName entrycget index optionβ returns the current value of a configuration option for the entry given byindex.optioncan be any of the names described in the MENU ENTRY OPTIONS section below. πpathName entryconfigure index ?options...?β same idea asconfigure, except it applies to the options for an individual entry, whileconfigureapplies to the menu as a whole.optionsmay have any of the values described in the MENU ENTRY OPTIONS section below. if options are specified, they get modified as indicated and the command returns an empty string. if no options are specified, it returns a list describing the current options for entryindex(seeTk_ConfigureInfofor the format). πpathName id indexβ returns the id of the menu entry given byindex. this is the identifier that got assigned to the entry when it was created using theaddorinsertwidget command. returns an empty string for the tear-off entry, or ifindexis equivalent to{}. π¦pathName index indexβ returns the numerical index corresponding toindex, or{}ifindexwas specified as{}. βpathName insert index type ?id? ?option value option value ...?β same as theaddwidget command, except it inserts the new entry just before the entry given byindexinstead of appending to the end of the menu.type,id,option, andvalueall mean the same thing as they do foradd. heads up though: it's not possible to insert new menu entries before the tear-off entry, if the menu has one. returns the id of the new entry. πΈpathName invoke indexβ invokes the action of the menu entry. peep the sections on the individual entries above for the deets on what actually happens. if the entry is disabled, nothing happens. if the entry has a command associated with it, the result of that command is returned as the result of theinvokewidget command β otherwise the result is an empty string. π£ note: invoking a menu entry does not automatically unpost the menu; the default bindings normally take care of that before invoking theinvokewidget command.pathName post x y ?index?β arranges for the menu to be displayed on screen at the root-window coordinates given byxandy. if anindexis specified, the menu gets located so that the entry with that index is displayed at the point. these coordinates get adjusted if necessary to guarantee the entire menu is visible on screen. normally returns an empty string. and if the-postcommandoption has been specified, its value is executed as a Tcl script before posting the menu, and the result of that script is returned as the result of thepostwidget command. if an error comes back while executing that command, the error is returned without posting the menu. π¨pathName postcascade indexβ posts the submenu associated with the cascade entry given byindex, and unposts any previously posted submenu. ifindexdoesn't correspond to a cascade entry, or ifpathNameisn't posted, the command has no effect except to unpost any currently posted submenu. πpathName type indexβ returns the type of the menu entry given byindex. that's thetypeargument you passed to theaddorinsertwidget command when the entry was created β likecommandorseparator, ortearofffor a tear-off entry. π₯pathName unpostβ unmaps the window so it's no longer displayed. if a lower-level cascaded menu is posted, it unposts that menu too. returns an empty string. β οΈ this subcommand does not work on Windows and the Macintosh, since those platforms have their own way of unposting menus. πpathName xposition indexβ returns a decimal string giving the x-coordinate within the menu window of the leftmost pixel in the entry specified byindex. πpathName yposition indexβ returns a decimal string giving the y-coordinate within the menu window of the topmost pixel in the entry specified byindex. π
π MENU ENTRY OPTIONS π
Okay so these are the options allowed on menu entries, bestie. Lowkey important: most options are not supported by all entry types. π
-activebackgroundvalue β picks the background color for showing this entry when itβs active. This one is ignored on Aqua/macOS. If you set it to an empty string (the default), then the overall menuβs-activebackgroundoption is used instead. If thetk_strictMotifvariable has been set to request strict Motif compliance, then this option is ignored and the-backgroundoption is used in its place. Not available for separator or tear-off entries. β¨-activeforegroundvalue β picks the foreground color for showing this entry when itβs active. Ignored on Aqua/macOS. If itβs an empty string (the default), then the overall menuβs-activeforegroundoption is used instead. π-acceleratorvalue β a string shown at the right side of the menu entry. Normally it describes an accelerator keystroke sequence that may be used to invoke the same function as the menu entry. This is just a display option β it does not actually set the corresponding binding (you can do that with thebindcommand). Not available for separator or tear-off entries. π¦-backgroundvalue β the background color for this entry when itβs in the normal state (neither active nor disabled). Ignored on Aqua/macOS. If itβs an empty string (the default), then the overall menuβs-backgroundoption is used instead. Not available for separator or tear-off entries. πΈ-bitmapvalue β a bitmap to show in the menu instead of a textual label, in any of the forms accepted byTk_GetBitmap. This overrides the-labeloption (as controlled by the-compoundoption), but may be reset to an empty string to enable a textual label to be displayed. If a-imageoption has been specified, it overrides-bitmap. Not available for separator or tear-off entries. π-columnbreakvalue β when this is zero, the entry appears below the previous entry. When itβs one, the entry appears at the top of a new column in the menu. This option is ignored on Aqua/macOS, where menus are always a single column. π«Ά-commandvalue β a Tcl command to execute when the menu entry is invoked. Not available for separator or tear-off entries. π-compoundvalue β says whether the menu entry should display both an image and text, and if so, where the image should go relative to the text. Valid values arebottom,center,left,none,rightandtop. The default isnone, meaning the button will display either an image or text, depending on the values of the-imageand-bitmapoptions. π₯-fontvalue β the font used when drawing the label or accelerator string in this entry. If itβs an empty string (the default), then the overall menuβs-fontoption is used instead. Not available for separator or tear-off entries. π-foregroundvalue β the foreground color for this entry when itβs in the normal state (neither active nor disabled). Ignored on Aqua/macOS. If itβs an empty string (the default), then the overall menuβs-foregroundoption is used instead. Not available for separator or tear-off entries. π-hidemarginvalue β says whether the standard margins should be drawn for this menu entry. This is useful when creating palette with images in them, i.e., color palettes, pattern palettes, etc.1indicates that the margin for the entry is hidden;0means that the margin is used. β-imagevalue β an image to show in the menu instead of a text string or bitmap. The image must have been created by some previous invocation ofimage create. This overrides the-labeland-bitmapoptions (as controlled by the-compoundoption), but may be reset to an empty string to enable a textual or bitmap label to be displayed. Not available for separator or tear-off entries. π-indicatoronvalue β available only for checkbutton and radiobutton entries. The value is a boolean that determines whether or not the indicator should be displayed. π-labelvalue β a string to display as an identifying label in the menu entry. Not available for separator or tear-off entries. π-menuvalue β available only for cascade entries. It specifies the path name of the submenu associated with this entry. The submenu must be a child of the menu. π§-offvaluevalue β available only for checkbutton entries. It specifies the value to store in the entryβs associated variable when the entry is deselected. π-onvaluevalue β available only for checkbutton entries. It specifies the value to store in the entryβs associated variable when the entry is selected. β¨-selectcolorvalue β available only for checkbutton and radiobutton entries. It specifies the color to display in the indicator when the entry is selected. If the value is an empty string (the default), then the-selectcoloroption for the menu determines the indicator color. π-selectimagevalue β available only for checkbutton and radiobutton entries. It specifies an image to display in the entry (in place of the-imageoption) when it is selected. The value is the name of an image, which must have been created by some previous invocation ofimage create. This option is ignored unless the-imageoption has been specified. π¦-statevalue β one of three states for the entry:normal,active, ordisabled. In normal state the entry is displayed using the-foregroundoption for the menu and the-backgroundoption from the entry or the menu. The active state is typically used when the pointer is over the entry. In active state the entry is displayed using the-activeforegroundoption for the menu along with the-activebackgroundoption from the entry. Disabled state means the entry should be insensitive: the default bindings will refuse to activate or invoke the entry. In this state the entry is displayed according to the-disabledforegroundoption for the menu and the-backgroundoption from the entry. This option is not available for separator entries. πΈ-underlinevalue β the integer index of a character to underline in the entry. This option is also queried by the default bindings and used to implement keyboard traversal.0corresponds to the first character of the text displayed in the entry,1to the next character, and so on. If a bitmap or image is displayed in the entry, then this option is ignored. Not available for separator or tear-off entries. π-valuevalue β available only for radiobutton entries. It specifies the value to store in the entryβs associated variable when the entry is selected. If an empty string is specified, then the-labeloption for the entry is used as the value to store in the variable. π«Ά-variablevalue β available only for checkbutton and radiobutton entries. It specifies the name of a global variable to set when the entry is selected. For checkbutton entries the variable is also set when the entry is deselected. For radiobutton entries, changing the variable causes the currently-selected entry to deselect itself.
For checkbutton entries, the default value of this option is taken from the -label option, and for radiobutton entries a single fixed value is used. It is recommended that you always set the -variable option when creating either a checkbutton or a radiobutton. π
π MENU CONFIGURATIONS π
The default bindings support four different ways of using menus:
π Pulldown Menus in Menubar
This is the most common case. You create a menu widget that will become the menu bar. You then add cascade entries to this menu, specifying the pull down menus you wish to use in your menu bar. You then create all of the pulldowns. Once you have done this, specify the menu using the -menu option of the toplevelβs widget command. See the toplevel manual entry for details. π
πΈ Pulldown Menus in Menu Buttons
This is the compatible way to do menu bars. You create one menubutton widget for each top-level menu, and typically you arrange a series of menubuttons in a row in a menubar window. You also create the top-level menus and any cascaded submenus, and tie them together with -menu options in menubuttons and cascade menu entries. The top-level menu must be a child of the menubutton, and each submenu must be a child of the menu that refers to it. Once you have done this, the default bindings will allow users to traverse and invoke the tree of menus via its menubutton; see the menubutton manual entry for details. β¨
π¦ Popup Menus
Popup menus typically post in response to a mouse button press or keystroke. You create the popup menus and any cascaded submenus, then you call the tk_popup procedure at the appropriate time to post the top-level menu. π
π Option Menus
An option menu consists of a menubutton with an associated menu that allows you to select one of several values. The current value is displayed in the menubutton and is also stored in a global variable. Use the tk_optionMenu procedure to create option menubuttons and their menus. π
π§ Torn-off Menus
You create a torn-off menu by invoking the tear-off entry at the top of an existing menu. The default bindings will create a new menu that is a copy of the original menu and leave it permanently posted as a top-level window. The torn-off menu behaves just the same as the original menu. π
β¨ DEFAULT BINDINGS β¨
Okay so Tk literally just goes ahead and creates class bindings for menus all by itself, which give them this default behavior β no cap, free of charge:
- When the mouse enters a menu, the entry underneath the mouse cursor activates; as the mouse moves around the menu, the active entry changes to track the mouse. It's giving follow-the-leader π
- When the mouse leaves a menu, all of the entries in the menu deactivate β except in the special case where the mouse moves from a menu to a cascaded submenu. That one's the exception, iykyk.
- When a button is released over a menu, the active entry (if any) is invoked. The menu also unposts unless it is a torn-off menu.
- The Space and Return keys invoke the active entry and unpost the menu. Two for one, we love efficiency π
- If any of the entries in a menu have letters underlined with the
-underlineoption, then pressing one of the underlined letters (or its upper-case or lower-case equivalent) invokes that entry and unposts the menu. Keyboard shortcuts, iconic. - The Escape key aborts a menu selection in progress without invoking any entry. It also unposts the menu unless it is a torn-off menu. Escape = the "nvm bestie" of keys π
- The Up and Down keys activate the next higher or lower entry in the menu. When one end of the menu is reached, the active entry wraps around to the other end. Round and round we go π
- The Left key moves to the next menu to the left. Here's the breakdown, stay with me:
- If the current menu is a cascaded submenu, then the submenu is unposted and the current menu entry becomes the cascade entry in the parent.
- If the current menu is a top-level menu posted from a menubutton, then the current menubutton is unposted and the next menubutton to the left is posted.
- Otherwise the key has no effect.
The left-right order of menubuttons is determined by their stacking order: Tk assumes that the lowest menubutton (which by default is the first one created) is on the left. So like, creation order = vibes order.
- The Right key moves to the next menu to the right. Detail time:
- If the current entry is a cascade entry, then the submenu is posted and the current menu entry becomes the first entry in the submenu.
- Otherwise, if the current menu was posted from a menubutton, then the current menubutton is unposted and the next menubutton to the right is posted.
Disabled menu entries are non-responsive: they do not activate and they ignore mouse button presses and releases. They're literally just there, looking pretty, doing nothing. Respect.
Several of the bindings make use of the command tk_menuSetFocus. It saves the current focus and sets the focus to its pathName argument, which is a menu widget.
And the plot twist: the behavior of menus can be changed by defining new bindings for individual widgets or by redefining the class bindings. Your menu, your rules π
π BUGS π
At present it is not possible to use the option database to specify values for the options to individual entries. Yeah. We know. It's giving tragic. Not me waiting for a fix π
π SEE ALSO π
bind(n)menubutton(n)ttk::menubutton(n)toplevel(n)
Go read those too, bestie β the whole friend group is linked up π«Ά
π KEYWORDS π
menu, widget
And that's a wrap on menu(n)! π This little masterpiece lives in the Tk world β the footer says version 4.1, page menu(n) β so go forth and post menus like the main character you are. Periodt. π
π¦ where this page comes from
- alpine 3.23: tk9-doc 9.0.3-r0 Β· TCL
- alpine 3.24: tk9-doc 9.0.3-r2 Β· TCL
- fedora 43: tk 1:9.0.2-1.fc43 Β· TCL AND HPND-Pbmplus AND CC-BY-SA-3.0 AND MIT-open-group AND MIT
- fedora 43: tk 1:9.0.2-1.fc43 Β· TCL AND HPND-Pbmplus AND CC-BY-SA-3.0 AND MIT-open-group AND MIT
- fedora 44: tk 1:9.0.2-1.fc44 Β· TCL AND HPND-Pbmplus AND CC-BY-SA-3.0 AND MIT-open-group AND MIT
- fedora 44: tk 1:9.0.2-1.fc44 Β· TCL AND HPND-Pbmplus AND CC-BY-SA-3.0 AND MIT-open-group AND MIT
- nixos 26.05: tk 9.0.3
- nixpkgs unstable: tk 9.1.0
- Copyright (c) 1990-1994 The Regents of the University of California
- Copyright (c) 1994-1997 Sun Microsystems, Inc
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms