menu(3tk)
๐ menu(3tk) โ Tk Built-In Commands ๐
okay bestie, gather round, because this is the menu(3tk) manual page and it's literally the blueprint for every drop-down, right-click popup, and menubar you've ever vibed with. no cap. ๐
โจ NAME โจ
menu, tk_menuSetFocus โ create and manipulate menu widgets and menubars. periodt. ๐
โจ SYNOPSIS โจ
menu pathName ?options?
tk_menuSetFocus pathName
โจ STANDARD OPTIONS โจ
here's the standard glow-up kit, all of these are options you can pass:
-activebackground-activeborderwidth-activeforeground-background-borderwidth-cursor-disabledforeground-font-foreground-relief-takefocus-activerelief
See the options manual entry for details on the standard options. iykyk. ๐
โจ WIDGET-SPECIFIC OPTIONS โจ
these are the ones that make a menu a menu, bestie:
-postcommandโ Database Name:postCommand; Database Class:Command. If this option is specified then it provides a Tcl command to execute each time the menu is posted. The command is invoked by thepostwidget command before posting the menu. Note that 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. This is due to the limitations in the individual platforms' menu managers. ๐ซถ-selectcolorโ Database Name:selectColor; Database Class:Background. For menu entries that are check buttons or radio buttons, this option specifies the color to display in the indicator when the check button or radio button is selected. โจ-tearoffโ Database Name:tearOff; Database Class:TearOff. This option must have a proper boolean value (default is false), which specifies whether or not the menu should include a tear-off entry at the top. If so, it will exist as entry 0 of the menu and the other entries will number starting at 1. The default menu bindings arrange for the menu to be torn off when the tear-off entry is invoked. This option is ignored under Aqua/macOS, where menus cannot be torn off. ๐-tearoffcommandโ Database Name:tearOffCommand; Database Class:TearOffCommand. If this option has a non-empty value, then it specifies a Tcl command to invoke whenever the menu is torn off. The actual command will consist of the value of this option, followed by a space, followed by the name of the menu window, followed by a space, followed by the name of the name of the torn off menu window. For example, if the option's value is โa bโ and menu.x.yis torn off to create a new menu.x.tearoff1, then the command โa b .x.y .x.tearoff1โ will be invoked. This option is ignored under Aqua/macOS, where menus cannot be torn off. ๐ธ-titleโ Database Name:title; Database Class:Title. The string will be used to title the window created when this menu is torn off. If the title is NULL, then the window will have the title of the menubutton or the text of the cascade item from which this menu was invoked. ๐-typeโ Database Name:type; Database Class:Type. This option can be one ofmenubar,tearoff, ornormal, and is set when the menu is created. While the string returned by the configuration database will change if this option is changed, this does not affect the menu widget's behavior. This is used by the cloning mechanism and is not normally set outside of the Tk library. ๐ฅ
โจ INTRODUCTION โจ
The menu command creates a new top-level window (given by the pathName argument) and makes it into a menu widget. That menu widget can either be used as a pop-up window or applied to a toplevel (with its -menu option) to make it into the menubar for that toplevel. Additional options, described above, may be specified on the command line or in the option database to configure aspects of the menu such as its colors and font. The menu command returns its pathName argument. At the time this command is invoked, there must not exist a window named pathName, but pathName's parent must exist. ๐ค
A menu is a widget that displays a collection of one-line entries arranged in one or more columns. There exist several different types of entries, each with different properties. Entries of different types may be combined in a single menu. Menu entries are not the same as entry widgets. In fact, menu entries are not even distinct widgets; the entire menu is one widget. ๐คฏ
Menu entries are displayed with up to three separate fields. The main field is a label in the form of a text string, a bitmap, or an image, controlled by the -label, -bitmap, and -image options for the entry. If the -accelerator option is specified for an entry then a second textual field is displayed 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. This is a display option, it does not actually set the corresponding binding (which can be achieved using the bind command). The third field is an indicator. The indicator is present only for checkbutton or radiobutton entries. It indicates whether the entry is selected or not, and is displayed to the left of the entry's string. โจ
In normal use, an entry becomes active (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. The effect of invocation is different for each type of entry; these effects are described below in the sections on individual entries. ๐
Entries may be disabled, which causes their labels and accelerators to be displayed with dimmer colors. The default menu bindings will not allow a disabled entry to be activated or invoked. Disabled entries may be re-enabled, at which point it becomes possible to activate and invoke them again. ๐
Whenever a menu's active entry is changed, a <<MenuSelect>> virtual event is sent to the menu. The active item can then be queried from the menu, and an action can be taken, such as setting context-sensitive help text for the entry. ๐
โจ TYPES OF ENTRIES โจ
๐ COMMAND ENTRIES ๐
The most common kind of menu entry is a command entry, which behaves much like a button widget. When a command entry is invoked, a Tcl command is executed. The Tcl command is specified with the -command option. ๐
๐ SEPARATOR ENTRIES ๐
A separator is an entry that is displayed as a horizontal dividing line. A separator may not be activated or invoked, and it has no behavior other than its display appearance. ๐
๐ CHECKBUTTON ENTRIES ๐
A checkbutton menu entry behaves much like a checkbutton widget. When it is invoked it toggles back and forth between the selected and deselected states. When the entry is selected, a particular value is stored in a particular global variable (as determined by the -onvalue and -variable options for the entry); when the entry is deselected another value (determined by the -offvalue option) is stored in the global variable. An indicator box is displayed to the left of the label in a checkbutton entry. If the entry is selected then 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. If a -command option is specified for a checkbutton entry, then its value is evaluated as a Tcl command each time the entry is invoked; this happens after toggling the entry's selected state. โ
๐ RADIOBUTTON ENTRIES ๐
A radiobutton menu entry behaves much like a radiobutton widget. Radiobutton entries are organized in groups of which only one entry 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. 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 are in the same group. An indicator diamond is displayed to the left of the label in each radiobutton entry. If the entry is selected then 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. If a -command option is specified for a radiobutton entry, then 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 allow the construction of cascading menus. 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). ๐ฆ
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. ๐
If a -command option is specified for a cascade entry then it is evaluated as a Tcl command whenever the entry is invoked. This is not supported on Windows. ๐
๐ TEAR-OFF ENTRIES ๐
A tear-off entry appears at the top of the menu if 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. 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. ๐
๐ MENUBARS ๐
okay so listen โ any menu can get promoted to being a menubar for a toplevel window. the syntax for that lives over in the toplevel command, go peek there bestie. โจ
what happens after depends on where you're running:
- on the Macintosh, whenever the toplevel is in front, this menu's cascade items show up in the menubar across the top of the main screen
- on Windows and Unix, this menu's items get displayed in a menubar across the top of the window
and these menus will behave according to the interface guidelines of their platforms, periodt. ๐
one very specific consequence of that: macOS requires that a menubar always be displayed for the active app. so if no menu is assigned by a configure -menu for a certain toplevel, a standard default menu will be displayed whenever that toplevel has focus. it just shows up. uninvited but iconic. ๐ซถ
heads up โ for every menu set as a menubar, a clone menu is made. more on that in the CLONES section, iykyk.
ngl menubars may behave differently on different platforms, and here's a prime example: 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. so no, your little checkbox isn't broken, the OS is just doing its thing. ๐
๐ธ SPECIAL MENUS IN MENUBARS ๐ธ
certain menus in a menubar get treated specially and honestly they're the main characters:
- 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.windowand.menubar.help - on Windows, the special menu would be
.menubar.system - on X Windows, the help menu would be
.menubar.help
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. ๐
lowkey important: 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. so timing is literally everything, bestie. ๐ญ
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. ๐ช
and the Window menu on the Mac also allows:
- toggling the window into a fullscreen state
- managing a tabbed window interface (multiple windows grouped into a single window) if supported by that version of the operating system
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. ๐ซถ
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. the API said no. ๐
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. slay. โจ
๐ CLONES ๐
so when does a clone happen? two moments, and they're both kind of a big deal:
- when a menu is set as a menubar for a toplevel window
- when a menu is torn off
in either case, a clone of the menu is made. ๐
here's the whole vibe of a clone:
- 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 (yes, they're synced like that ๐ช)
- additionally, any cascades that are pointed to are also cloned so that menu traversal will work right
and they don't live forever โ clones are destroyed when either the tearoff or menubar goes away, or when the original menu is destroyed. gone, but they ate while they were here. ๐
๐ WIDGET COMMAND ๐
okay bestie so when you call menu to build one of these, it creates a brand-new Tcl command and names it pathName. that's your remote control for the widget ๐ฎ โ you call it to do stuff to the menu. the general shape is:
pathName option ?arg ...?
option and the args you pass decide exactly what happens. no cap.
๐ INDEXES: how you point at an entry ๐
A whole bunch of the widget commands take one argument that says which entry of the menu you're operating on. these indicators are called indexes, and you can write them in any of these forms:
activeโ the entry that's currently active. if nothing is active, this form is equivalent to{}. this form may not be abbreviated.endโ the bottom-most entry in the menu. if the menu has no entries, this form is equivalent to{}. also may not be abbreviated.lastโ same asend. ๐ซถ{}โ "no entry at all". most commonly used with theactivateoption to deactivate every entry in the menu. in most cases, passing{}means nothing happens in the widget command.@x,yโ the entry covering the point in the menu's window given byxandy(pixel coordinates). if no entry covers that point, this form is equivalent to{}. if you only pass a single number, it gets treated as the y-coordinate. ๐numberโ the entry numerically, where0is the top-most entry,1is the one below it, and so on.idโ if the index doesn't satisfy any of the forms above, the menu gets searched for an entry with that id.patternโ if all the other ways of finding an entry fail, this one kicks in.patternis pattern-matched against the label of each entry in the menu, in order from the top down, until a matching entry is found. the rules ofstring matchare used. โจ
If an index could match more than one of those forms, the form that comes earlier in the list above takes precedence. Indexes also support the same simple interpretation as for the command string index, with simple integer index arithmetic and indexing relative to end.
here's everything you can actually do with a menu widget:
pathName activate indexโ changes the state of the entry indicated byindexto active and redisplays it using its active colors. any previously-active entry gets deactivated. ifindexis specified as{}, or if the entry you named is disabled, the menu ends up with no active entry. returns an empty string. ๐pathName add type ?id? ?option value option value ...?โ adds a new entry to the bottom of the menu. the new entry'stypeis given bytypeand must be one ofcascade,checkbutton,command,radiobutton, orseparator, or a unique abbreviation of one of those. if theidargument is specified, it's used as the entry identifier, andidmust not already exist in the menu. otherwise a new unique identifier gets generated. if there are extra arguments, they specify the options listed in the MENU ENTRY OPTIONS section below. theaddwidget command returns the id of the new entry. ๐pathName cget optionโ returns the current value of the configuration option given byoption.optionmay have any of the values accepted by themenucommand.pathName clone newPathname ?cloneType?โ makes a clone of the current menu namednewPathName. this clone is a menu in its own right, but any changes to the clone get 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 nooptionis specified, returns a list describing all the available options forpathName(seeTk_ConfigureInfofor info on the format of this list). ifoptionis specified with novalue, the command returns a list describing that one named option (and that list will be identical to the corresponding sublist of the value you'd get if nooptionwas specified). if one or moreoption-valuepairs are specified, the command modifies the given widget option(s) to have the given value(s) โ and in that case the command returns an empty string.optionmay have any of the values accepted by themenucommand.pathName delete index1 ?index2?โ deletes all the menu entries betweenindex1andindex2inclusive. ifindex2is omitted it defaults toindex1. โ ๏ธ attempts to delete a tear-off menu entry are 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.optionmay have any of the names described in the MENU ENTRY OPTIONS section below.pathName entryconfigure index ?options...?โ just like theconfigurecommand, except it applies to the options for an individual entry, whereasconfigureapplies to the options for the menu as a whole.optionsmay have any of the values described in the MENU ENTRY OPTIONS section below. ifoptionsare specified, the options are modified as indicated in the command and the command returns an empty string. if nooptionsare specified, it returns a list describing the current options for entryindex(seeTk_ConfigureInfofor info on the format of this list). ๐ฆpathName id indexโ returns the id of the menu entry given byindex. this is the identifier that was 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. thetype,id,option, andvaluearguments have the same interpretation as for theaddwidget command. โ ๏ธ it is not possible to insert new menu entries before the tear-off entry, if the menu has one. theinsertwidget command returns the id of the new entry. ๐pathName invoke indexโ invokes the action of the menu entry. see the sections on the individual entries above for details on what happens. if the menu entry is disabled then nothing happens. if the entry has a command associated with it, then 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 the screen at the root-window coordinates given byxandy. if anindexis specified, the menu will be 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 the screen. this command normally returns an empty string. 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 the command, then 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. this is thetypeargument that was 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, unpost 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 that live on the actual entries inside a menu. heads up bestie: not every entry type supports every one of these, so read the fine print on each, mwah ๐
-activebackgroundvalue โ the background color this entry wears when it's active. ignored on Aqua/macOS ๐ if you pass an empty string (that's the default), then the overall menu's-activebackgroundoption gets used instead. and iftk_strictMotifhas been set to request strict Motif compliance, this option is ignored and-backgroundis used in its place instead. not available for separator or tear-off entries.-activeforegroundvalue โ the foreground color for when the entry is active. ignored on Aqua/macOS. if it's an empty string (default), then the overall menu's-activeforegroundis used.-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. โ ๏ธ big one: this is 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 โ background color for this entry when it's in the normal state (neither active nor disabled). ignored on Aqua/macOS. empty string (default) โ the overall menu's-backgroundis used. not available for separator or tear-off entries.-bitmapvalue โ a bitmap to display 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 can be reset to an empty string to let a textual label show again. if a-imageoption has been specified, it overrides-bitmap. not available for separator or tear-off entries.-columnbreakvalue โ when it's 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. 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 โ whether the entry should display both an image and text, and if so, where the image sits relative to the text. valid values arebottom,center,left,none,rightandtop. default isnone, meaning the button displays 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. empty string (default) โ the overall menu's-fontis used. not available for separator or tear-off entries.-foregroundvalue โ foreground color for this entry in the normal state (neither active nor disabled). ignored on Aqua/macOS. empty string (default) โ the overall menu's-foregroundis used. not available for separator or tear-off entries.-hidemarginvalue โ whether the standard margins should be drawn for this menu entry. genuinely so useful when you're making a palette with images in them, i.e., color palettes, pattern palettes, etc. ๐จ1means the margin for the entry is hidden;0means the margin is used.-imagevalue โ an image to display 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 can be reset to an empty string to let a textual or bitmap label show. not available for separator or tear-off entries.-indicatoronvalue โ available only for checkbutton and radiobutton entries. it's a boolean that decides 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. 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. the value to store in the entry's associated variable when the entry is deselected.-onvaluevalue โ available only for checkbutton entries. the value to store in the entry's associated variable when the entry is selected.-selectcolorvalue โ available only for checkbutton and radiobutton entries. the color to display in the indicator when the entry is selected. if it's an empty string (the default), then the menu's-selectcoloroption determines the indicator color.-selectimagevalue โ available only for checkbutton and radiobutton entries. an image to display in the entry (in place of the-imageoption) when it's selected. the value is the name of an image, which must have been created by some previous invocation ofimage create. this is ignored unless the-imageoption has been specified.-statevalue โ one of three states for the entry:normal,active, ordisabled. innormalstate the entry is displayed using the menu's-foregroundoption and the-backgroundoption from the entry or the menu.activeis typically used when the pointer is over the entry โ the entry is displayed using the menu's-activeforegroundoption along with the-activebackgroundoption from the entry.disabledmeans the entry should be insensitive: the default bindings will refuse to activate or invoke the entry, and the entry is displayed according to the menu's-disabledforegroundoption 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. 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 as the value to store in the variable.-variablevalue โ available only for checkbutton and radiobutton entries. 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-labeloption, and for radiobutton entries a single fixed value is used. ๐ก it is recommended that you always set the-variableoption when creating either a checkbutton or a radiobutton.
๐ MENU CONFIGURATIONS ๐
the default bindings support four different ways of using menus (yes bestie, it says four and then lists five, we simply do not perceive it ๐):
๐ธ Pulldown Menus in Menubar ๐ธ
this is the most common case, fr fr. you create a menu widget that will become the menu bar. then you add cascade entries to this menu, specifying the pull down menus you wish to use in your menu bar. then you create all of the pulldowns. once you've done all that, 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've 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. periodt. ๐
โจ DEFAULT BINDINGS โจ
okay bestie, here's the tea ๐: Tk just automatically makes class bindings for menus, and they give you all this default behavior for free. no cap:
- when the mouse enters a menu, the entry underneath the mouse cursor activates ๐ and as the mouse moves around the menu, the active entry changes to track the mouse. it's giving follower energy fr fr.
- 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, don't forget it).
- when a button is released over a menu, the active entry (if there is one) gets invoked ๐ฅ. the menu also unposts unless it's a torn-off menu.
- the Space and Return keys invoke the active entry and unpost the menu. two birds, one slay.
- if any of the entries in a menu have letters underlined with the
-underlineoption, then pressing one of those underlined letters (or its upper-case or lower-case equivalent) invokes that entry and unposts the menu ๐ - the Escape key aborts a menu selection that's in progress, without invoking any entry at all. it also unposts the menu unless it's a torn-off menu. escape = the ultimate "nvm" ๐
- the Up and Down keys activate the next higher or lower entry in the menu. when you hit one end of the menu, the active entry wraps around to the other end ๐ iconic behavior honestly.
- the Left key moves to the next menu to the left. here's the breakdown:
- if the current menu is a cascaded submenu, then the submenu gets 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 gets unposted and the next menubutton to the left is posted
- otherwise? the key literally has no effect ๐
and 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 first created = leftmost, iykyk.
- the Right key moves to the next menu to the right. so:
- if the current entry is a cascade entry, then the submenu gets 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 gets unposted and the next menubutton to the right is posted
heads up bestie: disabled menu entries are non-responsive. they do not activate, and they ignore mouse button presses and releases. they're just there. vibing. contributing nothing. ๐ญ
several of the bindings make use of the command tk_menuSetFocus. what does it do? it saves the current focus and sets the focus to its pathName argument, which is a menu widget. short king behavior, honestly ๐ซถ
and the whole vibe is customizable: the behavior of menus can be changed by defining new bindings for individual widgets or by redefining the class bindings. you're the main character here, not Tk. ๐
๐ BUGS ๐
at present, it is not possible to use the option database to specify values for the options to individual entries. yeah. it's a whole thing. ngl this one hurts. ๐
๐ SEE ALSO ๐
bind(3tk)menubutton(3tk)ttk::menubutton(3tk)toplevel(3tk)
๐ KEYWORDS ๐
menuwidget
and that's a wrap on the whole page, bestie!! ๐๐ this was the Tk 4.1 version of the menu(3tk) manual page โ go forth, build your menus, and remember: every detail in here is facts, the sass is just seasoning ๐ธโจ bye ily ๐ซถ๐
๐ฆ where this page comes from
- ubuntu questing: tk9.0-doc 9.0.2-1
- ubuntu resolute: tk9.0-doc 9.0.3-1
- ubuntu stonking: tk9.0-doc 9.0.4-1
- 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 on : a language model's rewrite of the original, under its terms