wx.ListBox¶
A listbox is used to select one or more of a list of strings.
The strings are displayed in a scrolling box, with the selected string(s) marked in reverse video. A listbox can be single selection (if an item is selected, the previous selection is removed) or multiple selection (clicking an item toggles the item on or off independently of other selections).
List box elements are numbered from zero and while the maximal number of elements is unlimited, it is usually better to use a virtual control, not requiring to add all the items to it at once, such as wx.dataview.DataViewCtrl or wx.ListCtrl with LC_VIRTUAL
style, once more than a few hundreds items need to be displayed because this control is not optimized, neither from performance nor from user interface point of view, for large number of items.
Notice that the list box doesn’t support control characters other than TAB
.
Window Styles¶
This class supports the following styles:
wx.LB_SINGLE
: Single-selection list.wx.LB_MULTIPLE
: Multiple-selection list: the user can toggle multiple items on and off. This is the same aswx.LB_EXTENDED
in wxGTK2 port.wx.LB_EXTENDED
: Extended-selection list: the user can extend the selection by usingSHIFT
orCTRL
keys together with the cursor movement keys or the mouse.wx.LB_HSCROLL
: Create horizontal scrollbar if contents are too wide (Windows only).wx.LB_ALWAYS_SB
: Always show a vertical scrollbar.wx.LB_NEEDED_SB
: Only create a vertical scrollbar if needed.wx.LB_NO_SB
: Don’t create vertical scrollbar (wxMSW and wxGTK only).wx.LB_SORT
: The listbox contents are sorted in alphabetical order.
- Note that
LB_SINGLE
,LB_MULTIPLE
andLB_EXTENDED
styles are mutually exclusive and you can specify at most one of them (single selection is the default). See also Window Styles.
Events Emitted by this Class¶
Handlers bound for the following event types will receive a wx.CommandEvent parameter.
EVT_LISTBOX: Process a
wxEVT_LISTBOX
event, when an item on the list is selected or the selection changes.EVT_LISTBOX_DCLICK: Process a
wxEVT_LISTBOX_DCLICK
event, when the listbox is double-clicked. On some platforms (notably wxGTK2) pressing the enter key is handled as an equivalent of a double-click.
See also
wx.adv.EditableListBox, wx.Choice, wx.ComboBox, wx.ListCtrl, wx.CommandEvent
Class Hierarchy¶
Control Appearance¶
Known Subclasses¶
Methods Summary¶
Default constructor. |
|
Creates the listbox for two-step construction. |
|
Deselects an item in the list box. |
|
Ensure that the item with the given index is currently shown. |
|
Finds an item whose label matches the given string. |
|
Returns the number of items in the control. |
|
Return the number of items that can fit vertically in the visible area of the listbox. |
|
Returns the index of the selected item or |
|
Fill an array of ints with the positions of the currently selected items. |
|
Returns the label of the item with the given index. |
|
Return the index of the topmost visible item. |
|
Returns the item located at point, or |
|
Insert the given number of strings before the specified position. |
|
Determines whether an item is selected. |
|
Return |
|
Set the specified item to be the first visible item. |
|
Set the background colour of an item in the ListBox. |
|
Set the font of an item in the ListBox. |
|
Set the foreground colour of an item in the ListBox. |
|
Sets the selection to the given item n or removes the selection entirely if n == |
|
Sets the label for the given item. |
|
Properties Summary¶
See |
|
See |
|
See |
|
See |
|
See |
Class API¶
- class wx.ListBox(Control, ItemContainer)¶
Possible constructors:
ListBox() -> None ListBox(parent, id=ID_ANY, pos=DefaultPosition, size=DefaultSize, choices=[], style=0, validator=DefaultValidator, name=ListBoxNameStr) -> None
A listbox is used to select one or more of a list of strings.
Methods¶
- __init__(self, *args, **kw)¶
-
__init__ (self)
Default constructor.
- Return type:
None
__init__ (self, parent, id=ID_ANY, pos=DefaultPosition, size=DefaultSize, choices=[], style=0, validator=DefaultValidator, name=ListBoxNameStr)
Constructor, creating and showing a list box.
See the other wx.ListBox constructor; the only difference is that this overload takes a list of strings instead of a pointer to an array of String .
- Create(self, parent, id=ID_ANY, pos=DefaultPosition, size=DefaultSize, choices=[], style=0, validator=DefaultValidator, name=ListBoxNameStr)¶
Creates the listbox for two-step construction.
See wx.ListBox for further details.
- Parameters:
parent (wx.Window)
id (wx.WindowID)
pos (wx.Point)
size (wx.Size)
choices (list of strings)
style (long)
validator (wx.Validator)
name (string)
- Return type:
bool
- Deselect(self, n)¶
Deselects an item in the list box.
- Parameters:
n (int) – The zero-based item to deselect.
- Return type:
None
Note
This applies to multiple selection listboxes only.
- EnsureVisible(self, n)¶
Ensure that the item with the given index is currently shown.
This method scrolls the listbox only if necessary and doesn’t do anything if this item is already shown, unlike
SetFirstItem
.- Parameters:
n (int)
- Return type:
None
- FindString(self, string, caseSensitive=False)¶
Finds an item whose label matches the given string.
- Parameters:
string (string) – String to find.
caseSensitive (bool) – Whether search is case sensitive (default is not).
- Return type:
int
- Returns:
The zero-based position of the item, or
wx.NOT_FOUND
if the string was not found.
- static GetClassDefaultAttributes(variant=WINDOW_VARIANT_NORMAL)¶
- Parameters:
variant (WindowVariant)
- Return type:
- GetCount(self)¶
Returns the number of items in the control.
- Return type:
int
See also
IsEmpty
- GetCountPerPage(self)¶
Return the number of items that can fit vertically in the visible area of the listbox.
Returns -1 if the number of items per page couldn’t be determined. On wxGTK this method can only determine the number of items per page if there is at least one item in the listbox.
- Return type:
int
Added in version 4.1/wxWidgets-3.1.0.
- GetSelection(self)¶
Returns the index of the selected item or
NOT_FOUND
if no item is selected.- Return type:
int
- Returns:
The position of the current selection.
Note
This method can be used with single selection list boxes only, you must use
wx.ListBox.GetSelections
for the list boxes withwx.LB_MULTIPLE
style.See also
SetSelection
,GetStringSelection
- GetSelections(self)¶
Fill an array of ints with the positions of the currently selected items.
- Return type:
List[int]
- GetString(self, n)¶
Returns the label of the item with the given index.
The index must be valid, i.e. less than the value returned by
GetCount
, otherwise an assert is triggered. Notably, this function can’t be called if the control is empty.- Parameters:
n (int) – The zero-based index.
- Return type:
str
- Returns:
The label of the item.
- GetTopItem(self)¶
Return the index of the topmost visible item.
Returns
NOT_FOUND
if the method is not implemented for the current platform.- Return type:
int
Added in version 4.1/wxWidgets-3.1.0.
- HitTest(self, *args, **kw)¶
-
HitTest (self, point)
Returns the item located at point, or
NOT_FOUND
if there is no item located at point.It is currently implemented for wxMSW, Mac and wxGTK2 ports.
- Parameters:
point (wx.Point) – Point of item (in client coordinates) to obtain
- Return type:
int
- Returns:
Item located at point, or
wx.NOT_FOUND
if unimplemented or the item does not exist.
Added in version 2.7.0.
HitTest (self, x, y)
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
- Parameters:
x (int)
y (int)
- Return type:
int
- InsertItems(self, items, pos)¶
Insert the given number of strings before the specified position.
- Parameters:
items (list of strings) – Labels of items to be inserted
pos (int) – Position before which to insert the items: if pos is
0
the items will be inserted in the beginning of the listbox
- Return type:
None
- IsSelected(self, n)¶
Determines whether an item is selected.
- Parameters:
n (int) – The zero-based item index.
- Return type:
bool
- Returns:
True
if the given item is selected,False
otherwise.
- IsSorted(self)¶
Return
True
if the listbox hasLB_SORT
style.This method is mostly meant for internal use only.
- Return type:
bool
- MSWSetTabStops(self, tabStops)¶
- Return type:
None
- SetFirstItem(self, *args, **kw)¶
-
SetFirstItem (self, n)
Set the specified item to be the first visible item.
- Parameters:
n (int) – The zero-based item index that should be visible.
- Return type:
None
SetFirstItem (self, string)
Set the specified item to be the first visible item.
- Parameters:
string (string) – The string that should be visible.
- Return type:
None
- SetItemBackgroundColour(self, item, c)¶
Set the background colour of an item in the ListBox. Only valid on MSW and if the
wx.LB_OWNERDRAW
flag is set.- Return type:
None
- SetItemFont(self, item, f)¶
Set the font of an item in the ListBox. Only valid on MSW and if the
wx.LB_OWNERDRAW
flag is set.- Return type:
None
- SetItemForegroundColour(self, item, c)¶
Set the foreground colour of an item in the ListBox. Only valid on MSW and if the
wx.LB_OWNERDRAW
flag is set.- Return type:
None
- SetSelection(self, n)¶
Sets the selection to the given item n or removes the selection entirely if n ==
NOT_FOUND
.Note that this does not cause any command events to be emitted nor does it deselect any other items in the controls which support multiple selections.
- Parameters:
n (int) – The string position to select, starting from zero.
- Return type:
None
See also
- SetString(self, n, string)¶
Sets the label for the given item.
- Parameters:
n (int) – The zero-based item index.
string (string) – The label to set.
- Return type:
None
- SetStringSelection(self, *args, **kw)¶
-
SetStringSelection (self, s, select)
- Parameters:
s (string)
select (bool)
- Return type:
bool
SetStringSelection (self, s)
- Parameters:
s (string)
- Return type:
bool
Properties¶
- CountPerPage¶
See
GetCountPerPage
- Selection¶
See
GetSelection
andSetSelection
- Selections¶
See
GetSelections
- TopItem¶
See
GetTopItem