The widget roundup provides you with a quick and easy reference to the most important features
and options for each Tk widget.
Listbox
Widget Essentials
When to use: | To allow the user to choose one or more items from a possibly large fixed list of choices, in situations where screen real estate is not overly constrained. |
---|---|
Python: | listvar = StringVar(value=(items for listbox)) c = Listbox(parent, listvariable=listvar, height=10) |
Tcl: | set listvar [list items for listbox] tk::listbox .l -listvariable listvar -height 10 |
Ruby: | $listvar = TkVariable.new ( list of items for listbox ); l = TkListbox.new(parent) {listvariable $listvar; height 10} |
Perl: | my $listvar = Tcl formatted list of items for listbox; my $l = parent->new_tk__listbox( -listvariable => $listvar, height => 10); |
Reference: | (at www.tcl.tk) |
Common Options
listvariable | A variable that holds the list of items contained in the listbox; changing this variable will update the items in the listbox. |
---|---|
height | The number of rows high the listbox will request. |
selectmode | Whether the user can select only a single item (use a value of "browse"), or multiple items (use "extended"). |
yscrollcommand | Used to connect the listbox to a scrollbar's "set" method. |
How do I...
Find out which item(s) are currently selected? | Call the "curselection" method, which returns a list of the indices (0..n-1) of the selected items. |
---|---|
Change which items(s) are currently selected. | To deselect all items in a range, call "selection clear firstidx ?lastidx?". To select all items in a range, call "selection set firstidx ?lastidx?". |
Disable the listbox? | Set the "state" configuration option to "disabled". To re-enable, set the "state" configuration option to "normal". |
Find out what item is at a given index? | Look at that item in the list held in the variable named in the listvariable option. |
Make sure a particular item is in view? | Call the "see index" method. |
Tell when the selection in the listbox changes? | You can bind to the "<ListboxSelect>" virtual event. |