listbox
Guide: Listbox
Makes a listbox: a widget that shows a list of text items and lets the user select items.
listbox PATHNAME ?OPTION VALUE ...?
Description
Makes a listbox widget with the name PATHNAME and the options of the call, and makes the command PATHNAME for the widget commands below. Each item is one line of text. The user selects items with the mouse and the keys, by the rules of -selectmode, and curselection gives the indices of the selected items. With -listvariable, a global variable holds the list of the items.
The parent of PATHNAME must exist, and PATHNAME must not. A listbox is a Listbox in CSS and for the option database, and ztk::class gives it a style class for a stylesheet.
Many widget commands take an index of an item. An index is one of these forms:
- A number from 0. A number past the end gives the index of the end.
end: the place after the last item. Inget,delete, andactivate, it is the last item.active: the active item, whichactivateand the arrow keys set.anchor: the anchor of the selection, whichselection anchorand a click set.@X,Y: the item at the pixel X,Y in the listbox.
The selection of the listbox sends the virtual event <<ListboxSelect>> when the user changes it.
Widget commands
| Command | Description |
|---|---|
listbox activate | Makes the item at INDEX the active item. |
listbox bbox | Gives the box of the text of the item at INDEX. |
listbox curselection | Gives the indices of the selected items. |
listbox delete | Deletes the items from FIRST to LAST. |
listbox get | Gives the item at FIRST, or the items from FIRST to LAST. |
listbox index | Gives the number of the item that INDEX names. |
listbox insert | Inserts each ITEM before the item at INDEX. |
listbox itemcget | Gives the value of an option of one item. |
listbox itemconfigure | Gives or sets the options of one item. |
listbox nearest | Gives the index of the visible item nearest to the pixel row Y. |
listbox scan | Scrolls the listbox by a drag of the mouse. |
listbox see | Scrolls the listbox so that the item at INDEX is visible. |
listbox selection | Reads or changes the selected items. |
listbox size | Gives the count of the items. |
listbox xview | Reads or changes the horizontal view of items wider than the listbox. |
listbox yview | Reads or changes the vertical view of the items. |
listbox cget | Gives the value of an option of the listbox. |
listbox configure | Gives or sets the options of the listbox. |
Options
| Option | Type | Default | Database name | Description |
|---|---|---|---|---|
-activestyle | activestyle | dotbox | activeStyle | How the listbox marks its active item. |
-background | color | #ffffff | background | The background color of the listbox. |
-borderwidth | screen distance | 1 | borderWidth | The width of the border of the listbox. |
-cursor | cursor name | empty | cursor | The shape of the pointer over the listbox. |
-disabledforeground | color | #a3a3a3 | disabledForeground | The text color while the state is disabled. |
-exportselection | boolean | 1 | exportSelection | Whether the selection of the listbox is also the selection of the application. |
-font | font | TkDefaultFont | font | The font of the text. |
-foreground | color | #000000 | foreground | The color of the text. |
-height | integer | 10 | height | The height of the listbox in items. |
-highlightbackground | color | #d9d9d9 | highlightBackground | The color of the focus ring while the listbox has no focus, in the look of Tk on X11. |
-highlightcolor | color | #000000 | highlightColor | The color of the focus ring while the listbox has the focus, in the look of Tk on X11. |
-highlightthickness | screen distance | 1 | highlightThickness | The width of the focus ring, in the look of Tk on X11. |
-justify | justification | left | justify | The alignment of the items in the listbox. |
-relief | relief | sunken | relief | The 3-D look of the border. |
-selectbackground | color | #c3c3c3 | selectBackground | The background color of the selected text. |
-selectborderwidth | screen distance | 0 | selectBorderWidth | The width of the border around the selection. |
-selectforeground | color | #000000 | selectForeground | The text color of the selected text. |
-selectmode | string | browse | selectMode | How the mouse selects items. |
-setgrid | boolean | 0 | setGrid | Whether the listbox sets a grid for the size of its toplevel. |
-state | state | normal | state | Whether the user and a script can change the items and the selection. |
-takefocus | string | empty | takeFocus | Whether the Tab key can move the focus to the listbox. |
-width | integer | 20 | width | The width of the listbox in average characters of the font. |
-xscrollcommand | script | empty | xScrollCommand | A command prefix that the listbox calls when its horizontal view changes. |
-yscrollcommand | script | empty | yScrollCommand | A command prefix that the listbox calls when its vertical view changes. |
-listvariable | variable name | empty | listVariable | A global variable whose value is the list of the items. |
-emptytext | string | empty | emptyText | A text that the listbox shows while it has no items. |
-activestyle WORD
How the listbox marks its active item.
dotbox draws a dotted box around the active item, underline underlines it, and none draws no mark. The active item is the item of activate and of the keyboard.
Values: dotbox, none, underline.
% listbox .activestyle -activestyle none
> .activestyle cget -activestyle
none% listbox .activestyle2 -activestyle bogus
bad activestyle "bogus": must be dotbox, none, or underline
-errorcode TCL LOOKUP INDEX activestyle bogus-background COLOR
The background color of the listbox.
The color behind the content. -bg is the same option. A color is a name such as red, or #RGB, #RRGGBB, or #RRRRGGGGBBBB.

-background {#fde68a}
-background {#bfdbfe}-height 4 -width 12.% listbox .background -background {#336699}
> .background cget -background
#336699% listbox .background2 -background nocolor
unknown color name "nocolor"
-errorcode TK LOOKUP COLOR nocolor-borderwidth DISTANCE
The width of the border of the listbox.
The width of the border around the listbox. -bd is the same option. A distance is a count of pixels, or a number with c, i, m, or p for centimeters, inches, millimeters, or points.

-borderwidth 0
-borderwidth 1
-borderwidth 4-height 4 -width 12 -relief solid.% listbox .borderwidth -borderwidth 4
> .borderwidth cget -borderwidth
4% listbox .borderwidth2 -borderwidth x
bad screen distance "x"
-errorcode TK VALUE PIXELS-cursor CURSOR
The shape of the pointer over the listbox.
Each value side by side: the contact sheet of the cursors
A cursor name of Tk, such as hand2 or xterm. A value that starts with @ names a file. An empty value uses the cursor of the parent.
% listbox .cursor -cursor hand2
> .cursor cget -cursor
hand2% listbox .cursor2 -cursor nocursor
bad cursor spec "nocursor"
-errorcode TK VALUE CURSOR-disabledforeground COLOR
The text color while the state is disabled.
The color of the text while -state is disabled.

-disabledforeground {#888888}
-disabledforeground {#b91c1c}-height 4 -width 12 -state disabled.% listbox .disabledforeground -disabledforeground {#336699}
> .disabledforeground cget -disabledforeground
#336699% listbox .disabledforeground2 -disabledforeground nocolor
unknown color name "nocolor"
-errorcode TK LOOKUP COLOR nocolor-exportselection BOOLEAN
Whether the selection of the listbox is also the selection of the application.
In Tk, a true value makes a selection in the listbox the PRIMARY selection, and selection get gives it. ztk stores the value with no effect: the selection of the listbox is not the PRIMARY selection.
% listbox .exportselection -exportselection 0
> .exportselection cget -exportselection
0% listbox .exportselection2 -exportselection maybe
expected boolean value but got "maybe"
-errorcode TCL VALUE NUMBER-font FONT
The font of the text.
A font name, such as TkDefaultFont, or a font description, such as {Helvetica 12 bold}. A family that does not exist falls back to a font of the system.

-font TkDefaultFont
-font TkFixedFont
-font {Helvetica 16 bold}-height 4 -width 12.% listbox .font -font {Helvetica 12 bold}
> .font cget -font
Helvetica 12 bold% listbox .font2 -font {Helvetica big}
expected integer but got "big"
-errorcode TCL VALUE NUMBER-foreground COLOR
The color of the text.
The color of the text. -fg is the same option. A color is a name such as red, or #RGB, #RRGGBB, or #RRRRGGGGBBBB.

-foreground {#b91c1c}
-foreground {#2563eb}-height 4 -width 12.% listbox .foreground -foreground {#336699}
> .foreground cget -foreground
#336699% listbox .foreground2 -foreground nocolor
unknown color name "nocolor"
-errorcode TK LOOKUP COLOR nocolor-height LINES
The height of the listbox in items.
The listbox asks for room for this count of items. 0 or less makes the listbox as tall as all of its items.

-height 2
-height 4-width 12.% listbox .height -height 5
> .height cget -height
5% listbox .height2 -height x
expected integer but got "x"
-errorcode TCL VALUE NUMBER-highlightbackground COLOR
The color of the focus ring while the listbox has no focus, in the look of Tk on X11.
The color of the highlight ring of -highlightthickness while the listbox does not have the focus. ztk stores the value with no effect.
% listbox .highlightbackground -highlightbackground {#336699}
> .highlightbackground cget -highlightbackground
#336699% listbox .highlightbackground2 -highlightbackground nocolor
unknown color name "nocolor"
-errorcode TK LOOKUP COLOR nocolor-highlightcolor COLOR
The color of the focus ring while the listbox has the focus, in the look of Tk on X11.
The color of the highlight ring of -highlightthickness while the listbox has the focus. ztk stores the value with no effect.
% listbox .highlightcolor -highlightcolor {#336699}
> .highlightcolor cget -highlightcolor
#336699% listbox .highlightcolor2 -highlightcolor nocolor
unknown color name "nocolor"
-errorcode TK LOOKUP COLOR nocolor-highlightthickness DISTANCE
The width of the focus ring, in the look of Tk on X11.
The width of the ring around the listbox that shows the focus. The modern theme draws the focus with the stylesheet.
% listbox .highlightthickness -highlightthickness 4
> .highlightthickness cget -highlightthickness
4% listbox .highlightthickness2 -highlightthickness x
bad screen distance "x"
-errorcode TK VALUE PIXELS-justify SIDE
The alignment of the items in the listbox.
The place of each item in a listbox wider than the item: at the left side, in the middle, or at the right side.
Values: left, right, center.

-justify left
-justify center
-justify right-height 4 -width 12.% listbox .justify -justify right
> .justify cget -justify
right% listbox .justify2 -justify middle
bad justification "middle": must be left, right, or center
-errorcode TCL LOOKUP INDEX justification middle-relief RELIEF
The 3-D look of the border.
Each value side by side: the contact sheet of the reliefs
The style of the border: raised and sunken look 3-D, and groove and ridge are lines. The theme draws the border, and the CSS of the theme can give a relief its own look with border-style.
Values: flat, groove, raised, ridge, solid, sunken.

-relief flat
-relief raised
-relief sunken
-relief groove
-relief ridge
-relief solid-height 4 -width 12 -borderwidth 4. The modern theme draws each relief but flat with the same border.% listbox .relief -relief groove
> .relief cget -relief
groove% listbox .relief2 -relief bumpy
bad relief "bumpy": must be flat, groove, raised, ridge, solid, or sunken
-errorcode TCL LOOKUP INDEX relief bumpy-selectbackground COLOR
The background color of the selected text.
The color behind the characters of the selection. A color is a name such as red, or #RGB, #RRGGBB, or #RRRRGGGGBBBB.

-selectbackground {#fde68a}
-selectbackground {#bfdbfe}-height 4 -width 12.% listbox .selectbackground -selectbackground {#336699}
> .selectbackground cget -selectbackground
#336699% listbox .selectbackground2 -selectbackground nocolor
unknown color name "nocolor"
-errorcode TK LOOKUP COLOR nocolor-selectborderwidth DISTANCE
The width of the border around the selection.
The width of a 3-D border around each selected item. The rows of the listbox are higher by twice this width.
% listbox .selectborderwidth -selectborderwidth 4
> .selectborderwidth cget -selectborderwidth
4% listbox .selectborderwidth2 -selectborderwidth x
bad screen distance "x"
-errorcode TK VALUE PIXELS-selectforeground COLOR
The text color of the selected text.
The color of the characters of the selection.

-selectforeground {#b91c1c}
-selectforeground {#2563eb}-height 4 -width 12.% listbox .selectforeground -selectforeground {#336699}
> .selectforeground cget -selectforeground
#336699% listbox .selectforeground2 -selectforeground nocolor
unknown color name "nocolor"
-errorcode TK LOOKUP COLOR nocolor-selectmode MODE
How the mouse selects items.
browse and single allow one selected item. multiple makes each click add or remove an item. extended selects one item with a click, a range with a drag or Shift, and more items with Control. The modes change only the bindings of the mouse and the keys: selection set can select more items in each mode.
% listbox .selectmode -selectmode multiple
> .selectmode cget -selectmode
multipleztk stores each value with no check, as Tk 8.6.16 does.
-setgrid BOOLEAN
Whether the listbox sets a grid for the size of its toplevel.
In Tk, a true value makes the window manager resize the toplevel in steps of one line and one character. ztk stores the value with no effect.
% listbox .setgrid -setgrid 1
> .setgrid cget -setgrid
1% listbox .setgrid2 -setgrid maybe
expected boolean value but got "maybe"
-errorcode TCL VALUE NUMBER-state STATE
Whether the user and a script can change the items and the selection.
disabled blocks insert, delete, and the changes of the selection, and draws the text with -disabledforeground.
Values: disabled, normal.

-state normal
-state disabled-height 4 -width 12.% listbox .state -state disabled
> .state cget -state
disabled% listbox .state2 -state busy
bad state "busy": must be disabled or normal
-errorcode TCL LOOKUP INDEX state busy-takefocus TEXT
Whether the Tab key can move the focus to the listbox.
1 puts the listbox in the order of the Tab key, and 0 leaves it out. An empty value uses the default rule of Tk for the widget. In Tk the value can also be a script, and its result is the answer. ztk stores each value with no check.
% listbox .takefocus -takefocus 1
> .takefocus cget -takefocus
1ztk stores each value of the option with no check.
-width CHARS
The width of the listbox in average characters of the font.
0 or less makes the listbox as wide as its longest item.

-width 6
-width 12
-width 20-height 4.% listbox .width -width 5
> .width cget -width
5% listbox .width2 -width x
expected integer but got "x"
-errorcode TCL VALUE NUMBER-xscrollcommand SCRIPT
A command prefix that the listbox calls when its horizontal view changes.
The listbox calls the prefix with two fractions, the first and the last visible part of the content, from 0 to 1. The usual value is {.sb set} for a scrollbar .sb.
% listbox .xscrollcommand -xscrollcommand {set ::done 1}
> .xscrollcommand cget -xscrollcommand
set ::done 1ztk stores each value of the option with no check.
-yscrollcommand SCRIPT
A command prefix that the listbox calls when its vertical view changes.
The listbox calls the prefix with two fractions, the first and the last visible part of the content, from 0 to 1. The usual value is {.sb set} for a scrollbar .sb.
% listbox .yscrollcommand -yscrollcommand {set ::done 1}
> .yscrollcommand cget -yscrollcommand
set ::done 1ztk stores each value of the option with no check.
-listvariable VAR
A global variable whose value is the list of the items.
The listbox shows the elements of the list in the variable, and changes its items when the variable changes. insert and delete change the variable too.
% set fruits {apple pear plum}
> listbox .listvariable -listvariable fruits
> .listvariable get 0 end
apple pear plum
% listbox .listvariable2 -listvariable colors
> .listvariable2 insert end red blue
> set colors
red blueEach name is a valid variable name.
-emptytext TEXT
A text that the listbox shows while it has no items.
The listbox shows the text while it is empty. The text is not an item. This option is an addition of ztk.
% listbox .emptytext -emptytext {No items}
> .emptytext cget -emptytext
No itemsEach string is a valid text.
Returns
PATHNAME.
Examples
% listbox .fruits
> .fruits insert end apple pear plum
> .fruits get 0 end
apple pear plum
% listbox .colors -selectmode multiple
> .colors insert end red green blue
> .colors selection set 0
> .colors selection set 2
> .colors curselection
0 2
% winfo class [listbox .go]
Listbox
Errors
% listbox .x -foo 1
unknown option "-foo"
-errorcode TK LOOKUP OPTION -foo
% listbox .twice
> listbox .twice
window name "twice" already exists in parent
-errorcode TK VALUE WINDOW EXISTS
# The parent .no does not exist.
% listbox .no.b
bad window path name ".no"
-errorcode TK LOOKUP WINDOW .no
% listbox
wrong # args: should be "listbox pathName ?-option value ...?"
-errorcode TCL WRONGARGS
Differences from Tcl
- An error of a widget command, such as a bad index or a wrong count of arguments, has the
-errorcodeNONE. Tk 8.6.16 gives a code such asTK VALUE LISTBOX_INDEXorTCL WRONGARGS. xview scrollandyview scrollmove the view over 160 ms. Right after the command,nearestgives the item of the old view.- The selection of a listbox is not the
PRIMARYselection, soselection getgives an error. In Tk 8.6.16, with-exportselection 1,selection getgives the selected items. -exportselection,-setgrid,-highlightbackground, and-highlightcolorare stored with no effect.-emptytextis an addition of ztk. Tk 8.6.16 has no such option.- The rows have the height of the theme, so
bboxandnearestgive other values than Tk.
Facts
- Capability
- None.
- Tcl release
- Tcl 9.1 and 8.6.
- Since
- ztk 1.0.0-beta.1