beetlebug▾
/

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:

The selection of the listbox sends the virtual event <<ListboxSelect>> when the user changes it.

Widget commands

CommandDescription
listbox activateMakes the item at INDEX the active item.
listbox bboxGives the box of the text of the item at INDEX.
listbox curselectionGives the indices of the selected items.
listbox deleteDeletes the items from FIRST to LAST.
listbox getGives the item at FIRST, or the items from FIRST to LAST.
listbox indexGives the number of the item that INDEX names.
listbox insertInserts each ITEM before the item at INDEX.
listbox itemcgetGives the value of an option of one item.
listbox itemconfigureGives or sets the options of one item.
listbox nearestGives the index of the visible item nearest to the pixel row Y.
listbox scanScrolls the listbox by a drag of the mouse.
listbox seeScrolls the listbox so that the item at INDEX is visible.
listbox selectionReads or changes the selected items.
listbox sizeGives the count of the items.
listbox xviewReads or changes the horizontal view of items wider than the listbox.
listbox yviewReads or changes the vertical view of the items.
listbox cgetGives the value of an option of the listbox.
listbox configureGives or sets the options of the listbox.

Options

OptionTypeDefaultDatabase nameDescription
-activestyleactivestyledotboxactiveStyleHow the listbox marks its active item.
-backgroundcolor#ffffffbackgroundThe background color of the listbox.
-borderwidthscreen distance1borderWidthThe width of the border of the listbox.
-cursorcursor nameemptycursorThe shape of the pointer over the listbox.
-disabledforegroundcolor#a3a3a3disabledForegroundThe text color while the state is disabled.
-exportselectionboolean1exportSelectionWhether the selection of the listbox is also the selection of the application.
-fontfontTkDefaultFontfontThe font of the text.
-foregroundcolor#000000foregroundThe color of the text.
-heightinteger10heightThe height of the listbox in items.
-highlightbackgroundcolor#d9d9d9highlightBackgroundThe color of the focus ring while the listbox has no focus, in the look of Tk on X11.
-highlightcolorcolor#000000highlightColorThe color of the focus ring while the listbox has the focus, in the look of Tk on X11.
-highlightthicknessscreen distance1highlightThicknessThe width of the focus ring, in the look of Tk on X11.
-justifyjustificationleftjustifyThe alignment of the items in the listbox.
-reliefreliefsunkenreliefThe 3-D look of the border.
-selectbackgroundcolor#c3c3c3selectBackgroundThe background color of the selected text.
-selectborderwidthscreen distance0selectBorderWidthThe width of the border around the selection.
-selectforegroundcolor#000000selectForegroundThe text color of the selected text.
-selectmodestringbrowseselectModeHow the mouse selects items.
-setgridboolean0setGridWhether the listbox sets a grid for the size of its toplevel.
-statestatenormalstateWhether the user and a script can change the items and the selection.
-takefocusstringemptytakeFocusWhether the Tab key can move the focus to the listbox.
-widthinteger20widthThe width of the listbox in average characters of the font.
-xscrollcommandscriptemptyxScrollCommandA command prefix that the listbox calls when its horizontal view changes.
-yscrollcommandscriptemptyyScrollCommandA command prefix that the listbox calls when its vertical view changes.
-listvariablevariable nameemptylistVariableA global variable whose value is the list of the items.
-emptytextstringemptyemptyTextA 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.

listbox with -height 4 -width 12 and -background {#fde68a}-background {#fde68a}listbox with -height 4 -width 12 and -background {#bfdbfe}-background {#bfdbfe}
Each with -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.

listbox with -height 4 -width 12 -relief solid and -borderwidth 0-borderwidth 0listbox with -height 4 -width 12 -relief solid and -borderwidth 1-borderwidth 1listbox with -height 4 -width 12 -relief solid and -borderwidth 4-borderwidth 4
Each with -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.

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.

listbox with -height 4 -width 12 -state disabled and -disabledforeground {#888888}-disabledforeground {#888888}listbox with -height 4 -width 12 -state disabled and -disabledforeground {#b91c1c}-disabledforeground {#b91c1c}
Each with -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.

listbox with -height 4 -width 12 and -font TkDefaultFont-font TkDefaultFontlistbox with -height 4 -width 12 and -font TkFixedFont-font TkFixedFontlistbox with -height 4 -width 12 and -font {Helvetica 16 bold}-font {Helvetica 16 bold}
Each with -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.

listbox with -height 4 -width 12 and -foreground {#b91c1c}-foreground {#b91c1c}listbox with -height 4 -width 12 and -foreground {#2563eb}-foreground {#2563eb}
Each with -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.

listbox with -width 12 and -height 2-height 2listbox with -width 12 and -height 4-height 4
Each with -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.

listbox with -height 4 -width 12 and -justify left-justify leftlistbox with -height 4 -width 12 and -justify center-justify centerlistbox with -height 4 -width 12 and -justify right-justify right
Each with -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.

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.

listbox with -height 4 -width 12 -borderwidth 4 and -relief flat-relief flatlistbox with -height 4 -width 12 -borderwidth 4 and -relief raised-relief raisedlistbox with -height 4 -width 12 -borderwidth 4 and -relief sunken-relief sunkenlistbox with -height 4 -width 12 -borderwidth 4 and -relief groove-relief groovelistbox with -height 4 -width 12 -borderwidth 4 and -relief ridge-relief ridgelistbox with -height 4 -width 12 -borderwidth 4 and -relief solid-relief solid
Each with -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.

listbox with -height 4 -width 12 and -selectbackground {#fde68a}-selectbackground {#fde68a}listbox with -height 4 -width 12 and -selectbackground {#bfdbfe}-selectbackground {#bfdbfe}
Each with -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.

listbox with -height 4 -width 12 and -selectforeground {#b91c1c}-selectforeground {#b91c1c}listbox with -height 4 -width 12 and -selectforeground {#2563eb}-selectforeground {#2563eb}
Each with -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
multiple

ztk 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.

listbox with -height 4 -width 12 and -state normal-state normallistbox with -height 4 -width 12 and -state disabled-state disabled
Each with -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
1

ztk 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.

listbox with -height 4 and -width 6-width 6listbox with -height 4 and -width 12-width 12listbox with -height 4 and -width 20-width 20
Each with -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 1

ztk 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 1

ztk 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 blue

Each 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 items

Each 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

Facts

Capability
None.
Tcl release
Tcl 9.1 and 8.6.
Since
ztk 1.0.0-beta.1

See also