listbox(n)
๐ฆ listbox(n) โ Tk Built-In Commands ๐ฆ
okay so bestie, this is the listbox page and it's genuinely giving "list of strings, one per line" excellence ๐
every detail is staying, no cap. This is part 1 of 3, and we're not skipping a single flag. ๐ซถ
๐ธ NAME ๐ธ
listbox โ create and manipulate 'listbox' item list widgets โจ
โจ SYNOPSIS โจ
listbox pathName ?options?
you hand it a pathName, you can pass ?options?, and boom โ widget time ๐
๐ STANDARD OPTIONS ๐
these standard options are all in the group chat:
-background-borderwidth-cursor-disabledforeground-exportselection-font-foreground-highlightbackground-highlightcolor-highlightthickness-justify-relief-selectbackground-selectborderwidth-selectforeground-setgrid-takefocus-xscrollcommand-yscrollcommand
See the options manual entry for details on the standard options. ๐
๐ง WIDGET-SPECIFIC OPTIONS ๐ง
๐
-activestyle
- Command-Line Name:
-activestyle - Database Name:
activeStyle - Database Class:
ActiveStyle
okay so this one picks how the active element gets drawn. It must be one of dotbox (show a focus ring around the active element, cute), none (no special indication of active element, ghost mode) or underline (underline the active element). The default is underline on Windows, and dotbox elsewhere. slay. โจ
๐ -height
- Command-Line Name:
-height - Database Name:
height - Database Class:
Height
Specifies the desired height for the window, in lines. If zero or less, then the desired height for the window is made just large enough to hold all the elements in the listbox. so it's basically "fit the whole list" energy. ๐
๐ -listvariable
- Command-Line Name:
-listvariable - Database Name:
listVariable - Database Class:
Variable
Specifies the name of a global variable. The value of the variable is a list to be displayed inside the widget; if the variable value changes then the widget will automatically update itself to reflect the new value. Attempts to assign a variable with an invalid list value to -listvariable will cause an error. Attempts to unset a variable in use as a -listvariable will fail but will not generate an error. noted, bestie. ๐ซถ
๐ -selectmode
- Command-Line Name:
-selectmode - Database Name:
selectMode - Database Class:
SelectMode
Specifies one of several styles for manipulating the selection. The value of the option may be arbitrary, but the default bindings expect it to be either single, browse, multiple, or extended; the default value is browse. the bindings have opinions, honestly. ๐
๐ -state
- Command-Line Name:
-state - Database Name:
state - Database Class:
State
Specifies one of two states for the listbox: normal or disabled. If the listbox is disabled then items may not be inserted or deleted, items are drawn in the -disabledforeground color, and selection cannot be modified and is not shown (though selection information is retained). frozen but still keeping receipts, fr fr. ๐
๐ธ -width
- Command-Line Name:
-width - Database Name:
width - Database Class:
Width
Specifies the desired width for the window in characters. If the font does not have a uniform width then the width of the character 0 is used in translating from character units to screen units. If zero or less, then the desired width for the window is made just large enough to hold all the elements in the listbox. width math, but make it fashion. โจ
๐ฆ DESCRIPTION ๐ฆ
The listbox command creates a new window (given by the pathName argument) and makes it into a listbox widget. Additional options, described above, may be specified on the command line or in the option database to configure aspects of the listbox such as its colors, font, text, and relief. The listbox 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. ๐จ
bestie, no duplicate windows and no orphan windows โ the parent has to exist, periodt.
A listbox is a widget that displays a list of strings, one per line. When first created, a new listbox has no elements. Elements may be added or deleted using widget commands described below. In addition, one or more elements may be selected as described below. If a listbox is exporting its selection (see -exportselection option), then it will observe the standard X11 protocols for handling the selection. Listbox selections are available as type STRING; the value of the selection will be the text of the selected elements, with newlines separating the elements. X11 protocol girlies rise. ๐
It is not necessary for all the elements to be displayed in the listbox window at once; commands described below may be used to change the view in the window. Listboxes allow scrolling in both directions using the standard -xscrollcommand and -yscrollcommand options. They also support scanning, as described below. scroll girlies, we love you. ๐
๐ฎ INDICES ๐ฎ
Many of the widget commands for listboxes take one or more indices as arguments. An index specifies a particular element of the listbox, in any of the following ways: ๐ต๏ธโโ๏ธ
numberโ Specifies the element as a numerical index, where0corresponds to the first element in the listbox. ๐ขactiveโ Indicates the element that has the location cursor. This element will be displayed as specified by-activestylewhen the listbox has the keyboard focus, and it is specified with theactivatewidget command. ๐anchorโ Indicates the anchor point for the selection, which is set with theselection anchorwidget command. โendโ Indicates the end of the listbox. For most commands this refers to the last element in the listbox, but for a few commands such asindexandinsertit refers to the element just after the last one. ๐@x,yโ Indicates the element that covers the point in the listbox window specified byxandy(in pixel coordinates). If no element covers that point, then the closest element to that point is used. ๐
Indexes support the same simple interpretation as for the command string index, with simple integer index arithmetic and indexing relative to end. In the widget command descriptions below, arguments named index, first, and last always contain text indices in one of the above forms. ๐ง
๐ WIDGET COMMAND ๐
The listbox command creates a new Tcl command whose name is pathName. This command may be used to invoke various operations on the widget. It has the following general form: ๐
pathName option ?arg ...?
Option and the args determine the exact behavior of the command. The following commands are possible for listbox widgets: ๐
pathName activate indexโ Sets the active element to the one indicated byindex. Ifindexis outside the range of elements in the listbox then the closest element is activated. The active element is drawn as specified by-activestylewhen the widget has the input focus, and its index may be retrieved with theindex activecommand. cursor vibes. โจpathName bbox indexโ Returns a list of four numbers describing the bounding box of the text in the element given byindex. The first two elements of the list give thexandycoordinates of the upper-left corner of the screen area covered by the text (specified in pixels relative to the widget) and the last two elements give the width and height of the area, in pixels. If no part of the element given byindexis visible on the screen, or ifindexrefers to a non-existent element, then the result is an empty string; if the element is partially visible, the result gives the full area of the element, including any parts that are not visible. bounding box realness. ๐ฆpathName cget optionโ Returns the current value of the configuration option given byoption. Option may have any of the values accepted by thelistboxcommand. one-option peek. ๐pathName configure ?option? ?value option value ...?โ Query or modify the configuration options of the widget. If nooptionis specified, returns a list describing all of the available options forpathName(seeTk_ConfigureInfofor information on the format of this list). Ifoptionis specified with novalue, then the command returns a list describing the one named option (this list will be identical to the corresponding sublist of the value returned if nooptionis specified). If one or more option-value pairs are specified, then the command modifies the given widget option(s) to have the given value(s); in this case the command returns an empty string. Option may have any of the values accepted by thelistboxcommand. big config energy. โ๏ธpathName curselectionโ Returns a list containing the numerical indices of all of the elements in the listbox that are currently selected. If there are no elements selected in the listbox then an empty string is returned. who's selected? ๐pathName delete first ?last?โ Deletes one or more elements of the listbox.Firstandlastare indices specifying the first and last elements in the range to delete. Iflastis not specified it defaults tofirst, i.e. a single element is deleted. bye bestie. ๐๏ธpathName get first ?last?โ Iflastis omitted, returns the contents of the listbox element indicated byfirst, or an empty string iffirstrefers to a non-existent element. Iflastis specified, the command returns a list whose elements are all of the listbox elements betweenfirstandlast, inclusive. Bothfirstandlastmay have any of the standard forms for indices. receipts. ๐pathName index indexโ Returns the integer index value that corresponds toindex. Ifindexisendthe return value is a count of the number of elements in the listbox (not the index of the last element). sneaky but true. ๐ขpathName insert index ?element element ...?โ Inserts zero or more new elements in the list just before the element given byindex. Ifindexis specified asendthen the new elements are added to the end of the list. Returns an empty string. new elements, new me. โจpathName itemcget index optionโ Returns the current value of the item configuration option given byoption. Option may have any of the values accepted by theitemconfigurecommand. item-level peek, iykyk. ๐ท๏ธ
Okay so we're still in pathName's widget-command era, and this next drop is item glow-ups, scrolling lore, and selection drama. Stay with me bestie ๐
pathName itemconfigure index ?option? ?value? ?option value ...?
This one is literally the query-or-modify station for a single item in the listbox. If you don't pass any option, it returns a list describing all the available options for that item (peep Tk_ConfigureInfo for the format of that list, iykyk). If you pass an option with no value, it returns a list describing just that one named option, and that list is identical to the corresponding sublist of the value you'd get with no option specified. If you pass one or more option-value pairs, then the command modifies the given widget option(s) to have the given value(s), and in that case it returns an empty string. ๐ซถ
The following options are currently supported for items:
-backgroundcolor โ Color specifies the background color to use when displaying the item. It may have any of the forms accepted byTk_GetColor.-foregroundcolor โ Color specifies the foreground color to use when displaying the item. It may have any of the forms accepted byTk_GetColor.-selectbackgroundcolor โ color specifies the background color to use when displaying the item while it is selected. It may have any of the forms accepted byTk_GetColor.-selectforegroundcolor โ color specifies the foreground color to use when displaying the item while it is selected. It may have any of the forms accepted byTk_GetColor.
pathName nearest y โ give it a y-coordinate inside the listbox window, and it returns the index of the (visible) listbox element nearest to that y-coordinate. It's giving "which item is closest to this pixel vibe check" โจ
pathName scan option args โ this is the command that implements scanning on listboxes. It has two forms, depending on option:
pathName scan mark x yโ records x and y and the current view in the listbox window. Used in conjunction with laterscan dragtocommands. Typically this command is associated with a mouse button press in the widget. It returns an empty string. ๐ฑ๏ธpathName scan dragto x yโ computes the difference between its x and y arguments and the x and y arguments to the lastscan markcommand for the widget. It then adjusts the view by 10 times the difference in coordinates. This command is typically associated with mouse motion events in the widget, to produce the effect of dragging the list at high speed through the window. The return value is an empty string. Literally high-speed list drag energy ๐ฅ
pathName see index โ adjusts the view in the listbox so that the element given by index is visible. If the element is already visible then the command has no effect; if the element is near one edge of the window then the listbox scrolls to bring the element into view at the edge; otherwise the listbox scrolls to center the element. It's giving "make it visible, but make it make sense" ๐
pathName selection option arg โ this command is used to adjust the selection within a listbox. It has several forms, depending on option:
pathName selection anchor indexโ sets the selection anchor to the element given by index. If index refers to a non-existent element, then the closest element is used. The selection anchor is the end of the selection that is fixed while dragging out a selection with the mouse. The indexanchormay be used to refer to the anchor element. ๐pathName selection clear first ?last?โ if any of the elements between first and last (inclusive) are selected, they are deselected. The selection state is not changed for elements outside this range.pathName selection includes indexโ returns1if the element indicated by index is currently selected,0if it is not. Simple binary slay ๐pathName selection set first ?last?โ selects all of the elements in the range between first and last, inclusive, without affecting the selection state of elements outside that range.
pathName size โ returns a decimal string indicating the total number of elements in the listbox. Like a headcount but for list items ๐ง
pathName xview ?args โ this command is used to query and change the horizontal position of the information in the widget's window. It can take any of the following forms:
pathName xviewโ returns a list containing two elements. Each element is a real fraction between0and1; together they describe the horizontal span that is visible in the window. For example, if the first element is.2and the second element is.6, 20% of the listbox's text is off-screen to the left, the middle 40% is visible in the window, and 40% of the text is off-screen to the right. These are the same values passed to scrollbars via the-xscrollcommandoption.pathName xview indexโ adjusts the view in the window so that the character position given by index is displayed at the left edge of the window. Character positions are defined by the width of the character0.pathName xview moveto fractionโ adjusts the view in the window so that fraction of the total width of the listbox text is off-screen to the left. fraction must be a fraction between0and1.pathName xview scroll number whatโ this command shifts the view in the window left or right according to number and what. Number must be an integer or a float, but if it is a float then it is converted to an integer, rounded away from0. What must be eitherpagesorunitsor an abbreviation of one of these. If what ispagesthen the view adjusts by number screenfuls. If number is negative then characters farther to the left become visible; if it is positive then characters farther to the right become visible. If what isunits, the view adjusts left or right by number character units (the width of the0character) on the display. Horizontal scroll rights ๐
pathName yview ?args? โ this command is used to query and change the vertical position of the text in the widget's window. It can take any of the following forms:
pathName yviewโ returns a list containing two elements, both of which are real fractions between0and1. The first element gives the position of the listbox element at the top of the window, relative to the listbox as a whole (0.5means it is halfway through the listbox, for example). The second element gives the position of the listbox element just after the last one in the window, relative to the listbox as a whole. These are the same values passed to scrollbars via the-yscrollcommandoption.pathName yview indexโ adjusts the view in the window so that the element given by index is displayed at the top of the window.pathName yview moveto fractionโ adjusts the view in the window so that the element given by fraction appears at the top of the window. Fraction is a fraction between0and1;0indicates the first element in the listbox,0.33indicates the element one-third the way through the listbox, and so on.pathName yview scroll number whatโ this command adjusts the view in the window up or down according to number and what. Number must be an integer or a float, but if it is a float then it is converted to an integer, rounded away from0. What must be eitherpagesorunits. If what ispagesthen the view adjusts by number screenfuls. If number is negative then earlier elements become visible; if it is positive then later elements become visible. If what isunits, the view adjusts up or down by number lines. Vertical scroll supremacy ๐
โจ DEFAULT BINDINGS โจ
Okay so Tk literally just hands listboxes a whole set of class bindings โ automatic, no work from you โ and they give the widget that classic Motif-flavoured behavior ๐. And like, a huge chunk of how a listbox acts comes down to its -selectmode option, which picks one of four ways of dealing with the selection. Four. Whole. Personalities. ๐
If the selection mode is single or browse, at most one element can be selected in the listbox at once. In both modes, clicking button 1 on an element selects it and deselects any other selected item. In browse mode it is also possible to drag the selection with button 1. And on button 1, the listbox will also take focus if it has a normal state โ glow-up moment โจ.
If the selection mode is multiple or extended, any number of elements may be selected at once, including discontiguous ranges (yes bestie, non-adjacent picks, we love range chaos ๐). In multiple mode, clicking button 1 on an element toggles its selection state without affecting any other elements. In extended mode, pressing button 1 on an element selects it, deselects everything else, and sets the anchor to the element under the mouse; dragging the mouse with button 1 down extends the selection to include all the elements between the anchor and the element under the mouse, inclusive ๐ซถ.
Most people will probably want to use browse mode for single selections and extended mode for multiple selections; the other modes appear to be useful only in special situations. (ngl that's the tea โ those are the two main characters ๐)
Any time the set of selected item(s) in the listbox is updated by the user through the keyboard or mouse, the virtual event <<ListboxSelect>> will be generated. This virtual event will NOT be generated when adjusting the selection with the pathName selection command. It is easiest to bind to this event to be made aware of any user changes to listbox selection โ do that, fr fr ๐ฅ.
In addition to the above behavior, the following additional behavior is defined by the default bindings:
- In
extendedmode, the selected range can be adjusted by pressing button 1 with the Shift key down: this modifies the selection to consist of the elements between the anchor and the element under the mouse, inclusive. The un-anchored end of this new selection can also be dragged with the button down. ๐ฆ - In
extendedmode, pressing button 1 with the Control key down starts a toggle operation: the anchor is set to the element under the mouse, and its selection state is reversed. The selection state of other elements is not changed. If the mouse is dragged with button 1 down, then the selection state of all elements between the anchor and the element under the mouse is set to match that of the anchor element; the selection state of all other elements remains what it was before the toggle operation began. ๐ - If the mouse leaves the listbox window with button 1 down, the window scrolls away from the mouse, making information visible that used to be off-screen on the side of the mouse. The scrolling continues until the mouse re-enters the window, the button is released, or the end of the listbox is reached. (autoscroll go brrr ๐ญ)
- Mouse button 2 may be used for scanning. If it is pressed and dragged over the listbox, the contents of the listbox drag at high speed in the direction the mouse moves. ๐
- If the Up or Down key is pressed, the location cursor (active element) moves up or down one element. If the selection mode is
browseorextendedthen the new active element is also selected and all other elements are deselected. Inextendedmode the new active element becomes the selection anchor. โญ - In
extendedmode, Shift+Up and Shift+Down move the location cursor (active element) up or down one element and also extend the selection to that element in a fashion similar to dragging with mouse button 1. ๐ธ - The Left and Right keys scroll the listbox view left and right by the width of the character
0. Control+Left and Control+Right scroll the listbox view left and right by the width of the window. Control+Prior and Control+Next also scroll left and right by the width of the window. ๐ - The Prior and Next keys scroll the listbox view up and down by one page (the height of the window). ๐
- The Home and End keys scroll the listbox horizontally to the left and right edges, respectively. ๐ฆ
- Control+Home sets the location cursor to the first element in the listbox, selects that element, and deselects everything else in the listbox. ๐
- Control+End sets the location cursor to the last element in the listbox, selects that element, and deselects everything else in the listbox. ๐
- In
extendedmode, Control+Shift+Home extends the selection to the first element in the listbox and Control+Shift+End extends the selection to the last element. ๐ฅ - In
multiplemode, Control+Shift+Home moves the location cursor to the first element in the listbox and Control+Shift+End moves the location cursor to the last element. ๐ฅ - The space and Select keys make a selection at the location cursor (active element) just as if mouse button 1 had been pressed over this element. ๐ซถ
- In
extendedmode, Control+Shift+space and Shift+Select extend the selection to the active element just as if button 1 had been pressed with the Shift key down. ๐ - In
extendedmode, the Escape key cancels the most recent selection and restores all the elements in the selected range to their previous selection state. (undo but make it selection ๐) - Control+/ selects everything in the widget, except in
singleandbrowsemodes, in which case it selects the active element and deselects everything else. โจ - Control+backslash deselects everything in the widget, except in
browsemode where it has no effect. ๐ - The
F16key (labelledCopyon many Sun workstations) or Meta+w copies the selection in the widget to the clipboard, if there is a selection. ๐
The behavior of listboxes can be changed by defining new bindings for individual widgets or by redefining the class bindings. So yeah โ remix it, bestie, the defaults are just the defaults ๐ง
๐ SEE ALSO ๐
ttk::treeview(n)
๐ KEYWORDS ๐
listbox, widget
That's the whole page, bestie โ you're officially listbox-literate ๐๐
This was listbox(n) from Tk 8.4 (the version/source line at the bottom, iykyk), and yes it's an older-era page but the vibes are timeless โญ Go forth and select things โจ๐ซถ
๐ฆ 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
- Copyright (c) 1990 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