ListBox#
Superclasses: Widget, InitiallyUnowned, Object
Implemented Interfaces: Accessible, Buildable, ConstraintTarget
GtkListBox is a vertical list.
A GtkListBox only contains GtkListBoxRow children. These rows can
by dynamically sorted and filtered, and headers can be added dynamically
depending on the row content. It also allows keyboard and mouse navigation
and selection like a typical list.
Using GtkListBox is often an alternative to GtkTreeView, especially
when the list contents has a more complicated layout than what is allowed
by a GtkCellRenderer, or when the contents is interactive (i.e. has a
button in it).
Although a GtkListBox must have only GtkListBoxRow children, you can
add any kind of widget to it via prepend,
append and insert and a
GtkListBoxRow widget will automatically be inserted between the list
and the widget.
GtkListBoxRows can be marked as activatable or selectable. If a row is
activatable, row_activated will be emitted for it when
the user tries to activate it. If it is selectable, the row will be marked
as selected when the user tries to select it.
GtkListBox as GtkBuildable#
The GtkListBox implementation of the GtkBuildable interface supports
setting a child as the placeholder by specifying “placeholder” as the “type”
attribute of a <child> element. See set_placeholder
for info.
Shortcuts and Gestures#
The following signals have default keybindings:
CSS nodes#
list[.separators][.rich-list][.navigation-sidebar][.boxed-list]
╰── row[.activatable]
GtkListBox uses a single CSS node named list. It may carry the .separators
style class, when the show_separators property is set.
Each GtkListBoxRow uses a single CSS node named row. The row nodes get the
.activatable style class added when appropriate.
It may also carry the .boxed-list style class. In this case, the list will be automatically surrounded by a frame and have separators.
The main list node may also carry style classes to select the style of list presentation: .rich-list, .navigation-sidebar or .data-table.
Accessibility#
GtkListBox uses the LIST role and GtkListBoxRow uses
the LIST_ITEM role.
Constructors#
Methods#
- class ListBox
- append(child: Widget) None#
Append a widget to the list.
If a sort function is set, the widget will actually be inserted at the calculated position.
- Parameters:
child – the
GtkWidgetto add
- bind_model(model: ListModel | None = None, create_widget_func: Callable[[Object, Any], Widget] | None = None, user_data: Any = None) None#
Binds
modeltobox.If
boxwas already bound to a model, that previous binding is destroyed.The contents of
boxare cleared and then filled with widgets that represent items frommodel.boxis updated whenevermodelchanges. IfmodelisNone,boxis left empty.It is undefined to add or remove widgets directly (for example, with
insert) whileboxis bound to a model.Note that using a model is incompatible with the filtering and sorting functionality in
GtkListBox. When using a model, filtering and sorting should be implemented by the model.- Parameters:
model – the
GListModelto be bound toboxcreate_widget_func – a function that creates widgets for items or
Nonein case you also passedNoneasmodeluser_data – user data passed to
create_widget_func
- drag_highlight_row(row: ListBoxRow) None#
Add a drag highlight to a row.
This is a helper function for implementing DnD onto a
GtkListBox. The passed inrowwill be highlighted by setting theDROP_ACTIVEstate and any previously highlighted row will be unhighlighted.The row will also be unhighlighted when the widget gets a drag leave event.
- Parameters:
row – a
GtkListBoxRow
- drag_unhighlight_row() None#
If a row has previously been highlighted via
drag_highlight_row(), it will have the highlight removed.
- get_adjustment() Adjustment | None#
Gets the adjustment (if any) that the widget uses to for vertical scrolling.
- get_row_at_index(index_: int) ListBoxRow | None#
Gets the n-th child in the list (not counting headers).
If
index_is negative or larger than the number of items in the list,Noneis returned.- Parameters:
index – the index of the row
- get_row_at_y(y: int) ListBoxRow | None#
Gets the row at the
yposition.- Parameters:
y – position
- get_selected_row() ListBoxRow | None#
Gets the selected row, or
Noneif no rows are selected.Note that the box may allow multiple selection, in which case you should use
selected_foreachto find all selected rows.
- get_selected_rows() list[ListBoxRow]#
Creates a list of all selected children.
- get_selection_mode() SelectionMode#
Gets the selection mode of the listbox.
- insert(child: Widget, position: int) None#
Insert the
childinto theboxatposition.If a sort function is set, the widget will actually be inserted at the calculated position.
If
positionis -1, or larger than the total number of items in thebox, then thechildwill be appended to the end.- Parameters:
child – the
GtkWidgetto addposition – the position to insert
childin
- invalidate_filter() None#
Update the filtering for all rows.
Call this when result of the filter function on the
boxis changed due to an external factor. For instance, this would be used if the filter function just looked for a specific search string and the entry with the search string has changed.
- invalidate_headers() None#
Update the separators for all rows.
Call this when result of the header function on the
boxis changed due to an external factor.
- invalidate_sort() None#
Update the sorting for all rows.
Call this when result of the sort function on the
boxis changed due to an external factor.
- prepend(child: Widget) None#
Prepend a widget to the list.
If a sort function is set, the widget will actually be inserted at the calculated position.
- Parameters:
child – the
GtkWidgetto add
- remove_all() None#
Removes all rows from
box.This function does nothing if
boxis backed by a model.Added in version 4.12.
- select_row(row: ListBoxRow | None = None) None#
Make
rowthe currently selected row.- Parameters:
row – The row to select
- selected_foreach(func: Callable[[ListBox, ListBoxRow, Any], None], data: Any = None) None#
Calls a function for each selected child.
Note that the selection cannot be modified from within this function.
- Parameters:
func – the function to call for each selected child
data – user data to pass to the function
- set_activate_on_single_click(single: bool) None#
If
singleisTrue, rows will be activated when you click on them, otherwise you need to double-click.- Parameters:
single – a boolean
- set_adjustment(adjustment: Adjustment | None = None) None#
Sets the adjustment (if any) that the widget uses to for vertical scrolling.
For instance, this is used to get the page size for PageUp/Down key handling.
In the normal case when the
boxis packed inside aGtkScrolledWindowthe adjustment from that will be picked up automatically, so there is no need to manually do that.- Parameters:
adjustment – the adjustment
- set_filter_func(filter_func: Callable[[ListBoxRow, Any], bool] | None = None, user_data: Any = None) None#
By setting a filter function on the
boxone can decide dynamically which of the rows to show.For instance, to implement a search function on a list that filters the original list to only show the matching rows.
The
filter_funcwill be called for each row after the call, and it will continue to be called each time a row changes (viachanged) or wheninvalidate_filteris called.Note that using a filter function is incompatible with using a model (see
bind_model).- Parameters:
filter_func – callback that lets you filter which rows to show
user_data – user data passed to
filter_func
- set_header_func(update_header: Callable[[ListBoxRow, ListBoxRow | None, Any], None] | None = None, user_data: Any = None) None#
Sets a header function.
By setting a header function on the
boxone can dynamically add headers in front of rows, depending on the contents of the row and its position in the list.For instance, one could use it to add headers in front of the first item of a new kind, in a list sorted by the kind.
The
update_headercan look at the current header widget usingget_headerand either update the state of the widget as needed, or set a new one usingset_header. If no header is needed, set the header toNone.Note that you may get many calls
update_headerto this for a particular row when e.g. changing things that don’t affect the header. In this case it is important for performance to not blindly replace an existing header with an identical one.The
update_headerfunction will be called for each row after the call, and it will continue to be called each time a row changes (viachanged) and when the row before changes (either bychangedon the previous row, or when the previous row becomes a different row). It is also called for all rows wheninvalidate_headersis called.- Parameters:
update_header – callback that lets you add row headers
user_data – user data passed to
update_header
- set_placeholder(placeholder: Widget | None = None) None#
Sets the placeholder widget that is shown in the list when it doesn’t display any visible children.
- Parameters:
placeholder – a
GtkWidget
- set_selection_mode(mode: SelectionMode) None#
Sets how selection works in the listbox.
- Parameters:
mode – The
GtkSelectionMode
- set_show_separators(show_separators: bool) None#
Sets whether the list box should show separators between rows.
- Parameters:
show_separators –
Trueto show separators
- set_sort_func(sort_func: Callable[[ListBoxRow, ListBoxRow, Any], int] | None = None, user_data: Any = None) None#
Sets a sort function.
By setting a sort function on the
boxone can dynamically reorder the rows of the list, based on the contents of the rows.The
sort_funcwill be called for each row after the call, and will continue to be called each time a row changes (viachanged) and wheninvalidate_sortis called.Note that using a sort function is incompatible with using a model (see
bind_model).- Parameters:
sort_func – the sort function
user_data – user data passed to
sort_func
- unselect_row(row: ListBoxRow) None#
Unselects a single row of
box, if the selection mode allows it.- Parameters:
row – the row to unselect
Properties#
Signals#
- class ListBox.signals
-
- move_cursor(step: MovementStep, count: int, extend: bool, modify: bool) None#
The type of the None singleton.
- Parameters:
step – the granularity of the move, as a
GtkMovementStepcount – the number of
stepunits to moveextend – whether to extend the selection
modify – whether to modify the selection
- row_activated(row: ListBoxRow) None#
The type of the None singleton.
- Parameters:
row – the activated row
- row_selected(row: ListBoxRow | None = None) None#
The type of the None singleton.
- Parameters:
row – the selected row