Documentation edit
Other Listboxes edit
- BWidget's ListBox
- BLT
- well, some of the add ons for BLT, anyways
- ClassyTk
- NexTk
- TLC
- Tix
- Gnocl
- virtuallist
- mutable, can be larger than available memory, in-memory cache for performance, items can be invalidated to free memory, metadata can be associated with each item
- xlistbox
Example edit
D. McC: A simple example of the use of listbox (modified from the previous example, so that the scrollbar now works#! /usr/bin/env wish listbox .lb -selectmode multiple -height 4 scrollbar .sb -command [list .lb yview] .lb configure -yscrollcommand [list .sb set] .lb insert 0 sample stuff colors red yellow green grid .lb .sb -sticky news grid columnconfigure . 0 -weight 1 grid rowconfigure . 0 -weight 1 .lb itemconfigure 0 -foreground blue .lb itemconfigure 1 -foreground white .lb itemconfigure 2 -foreground black .lb itemconfigure 3 -foreground red .lb itemconfigure 4 -foreground yellow .lb itemconfigure 5 -foreground greenLV: So, once .lb is created, and someone selects one of the entries, then what? How would a program make use of the item selected?There's a listbox subcommand called curselection. This provides you a list of indices selected. But is there a way one can get the text of the entry selected? Or does one have to create some sort of data structure, along with code to modify the data structure every time a user action results in a modified list?
Often someone asks how to get a listbox to take more than one selection and people recommend they read the above reference page's information on -exportselection 0 .CJU: Normally, if you have more than one listbox on the screen, selecting one listbox automatically unselects any previous selections in the all of the others. -exportselection 0 is a way of getting around that at the cost of not being able to export the list element(s) to the X selection. I understand the reasoning for this behavior, but I think the default for -exportselection should be 0 instead of 1 for the listbox simply due to the confusion it can cause for newcomers of Tk and also the relative rarity of the need to copy from a listbox as opposed to a text or entry widget, for example.MGS: Note: selection is not the same as the clipboard. See: selectionCJU: Corrected it.See: listbox selection
Tk's venerable FAQ covers several aspects of listbox management which are, in fact, frequently-asked. Among these are synchronization between parallel listboxes, multi-selection, ... [1].Also, note that people who start with listboxes often want to move to comboboxes, wcb, multicolumn listboxes (like tablelist), hugecombo, Hugelist, virtuallist...
LV: Has anyone implemented a listbox with separators, for cases where you want to visually group related members of a listbox?MHo: With BWidget's ListBox it's at least possible, with the help of the -window option, to draw a line which visually acts as a separator. The -text could be "" then. Seems the line is not clickable (but selectable via keyboard... - maybe some bindings would help). I'm in search for such a solution, too.
RS 2006-12-11: Mousewheel is supported by Tk (on Win XP at least), but in a surprising way: focus is needed, but can't be applied by clicking into a listbox, even with the -takefocus 1 option. After an explicit
focus .lbthe widget border is highlighted, and the mousewheel can be used to scroll up and down. To get "focus follows click", you can add a binding like this:
bind .lb <1> {+ focus %W}
See Also edit
- How to totally handle listbox selection
- Manage a button so that it's only enabled when something is selected in the listbox.
- Listbox Drag-and-Drop
- Re-order listbox elements by dragging and dropping.