Gtk.ListBox¶
class — extends Widget, Accessible, Buildable, ConstraintTarget
Shows a vertical list.
<picture> <source srcset="list-box-dark.png" media="(prefers-color-scheme: dark)"> <img alt="An example GtkListBox" src="list-box.png"> </picture>
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 ListBox.prepend,
ListBox.append and ListBox.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, ListBox.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 ListBox.set_placeholder
for info.
Shortcuts and Gestures¶
The following signals have default keybindings:
CSS nodes¶
GtkListBox uses a single CSS node named list. It may carry the .separators
style class, when the ListBox.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 AccessibleRole.list role and GtkListBoxRow uses
the AccessibleRole.list_item role.
Constructors¶
new¶
Creates a new GtkListBox container.
Methods¶
append¶
Append a widget to the list.
If a sort function is set, the widget will actually be inserted at the calculated position.
Parameters:
child— theGtkWidgetto add
bind_model¶
def bind_model(self, model: Gio.ListModel | None = ..., create_widget_func: ListBoxCreateWidgetFunc | None = ...) -> None
Binds model to box.
If box was already bound to a model, that previous binding is
destroyed.
The contents of box are cleared and then filled with widgets that
represent items from model. box is updated whenever model changes.
If model is None, box is left empty.
It is undefined to add or remove widgets directly (for example, with
ListBox.insert) while box is 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— theGListModelto be bound toboxcreate_widget_func— a function that creates widgets for items orNonein case you also passedNoneasmodel
drag_highlight_row¶
Add a drag highlight to a row.
This is a helper function for implementing DnD onto a GtkListBox.
The passed in row will be highlighted by setting the
StateFlags.DROP_ACTIVE state and any previously highlighted
row will be unhighlighted.
The row will also be unhighlighted when the widget gets a drag leave event.
Parameters:
row— aGtkListBoxRow
drag_unhighlight_row¶
If a row has previously been highlighted via ListBox.drag_highlight_row,
it will have the highlight removed.
get_activate_on_single_click¶
Returns whether rows activate on single clicks.
get_adjustment¶
Gets the adjustment (if any) that the widget uses to for vertical scrolling.
get_row_at_index¶
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, None is returned.
Parameters:
index_— the index of the row
get_row_at_y¶
Gets the row at the y position.
Parameters:
y— position
get_selected_row¶
Gets the selected row, or None if no rows are selected.
Note that the box may allow multiple selection, in which
case you should use ListBox.selected_foreach to
find all selected rows.
get_selected_rows¶
Creates a list of all selected children.
get_selection_mode¶
Gets the selection mode of the listbox.
get_show_separators¶
Returns whether the list box should show separators between rows.
get_tab_behavior¶
Returns the behavior of the <kbd>Tab</kbd> and <kbd>Shift</kbd>+<kbd>Tab</kbd> keys.
insert¶
Insert the child into the box at position.
If a sort function is set, the widget will actually be inserted at the calculated position.
If position is -1, or larger than the total number of items in the
box, then the child will be appended to the end.
Parameters:
child— theGtkWidgetto addposition— the position to insertchildin
invalidate_filter¶
Update the filtering for all rows.
Call this when result
of the filter function on the box is 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¶
Update the separators for all rows.
Call this when result
of the header function on the box is changed due
to an external factor.
invalidate_sort¶
Update the sorting for all rows.
Call this when result
of the sort function on the box is changed due
to an external factor.
prepend¶
Prepend a widget to the list.
If a sort function is set, the widget will actually be inserted at the calculated position.
Parameters:
child— theGtkWidgetto add
remove¶
Removes a child from box.
Parameters:
child— the child to remove
remove_all¶
Removes all rows from box.
This function does nothing if box is backed by a model.
select_all¶
Select all children of box, if the selection mode allows it.
select_row¶
Make row the currently selected row.
Parameters:
row— The row to select
selected_foreach¶
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
set_activate_on_single_click¶
If single is True, rows will be activated when you click on them,
otherwise you need to double-click.
Parameters:
single— a boolean
set_adjustment¶
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 box is packed inside
a GtkScrolledWindow the adjustment from that will
be picked up automatically, so there is no need
to manually do that.
Parameters:
adjustment— the adjustment
set_filter_func¶
By setting a filter function on the box one 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_func will be called for each row after the call, and
it will continue to be called each time a row changes (via
ListBoxRow.changed) or when ListBox.invalidate_filter
is called.
Note that using a filter function is incompatible with using a model
(see ListBox.bind_model).
Parameters:
filter_func— callback that lets you filter which rows to show
set_header_func¶
Sets a header function.
By setting a header function on the box one 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_header can look at the current header widget using
ListBoxRow.get_header and either update the state of the widget
as needed, or set a new one using ListBoxRow.set_header. If no
header is needed, set the header to None.
Note that you may get many calls update_header to 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_header function will be called for each row after the call,
and it will continue to be called each time a row changes (via
ListBoxRow.changed) and when the row before changes (either
by ListBoxRow.changed on the previous row, or when the previous
row becomes a different row). It is also called for all rows when
ListBox.invalidate_headers is called.
Parameters:
update_header— callback that lets you add row headers
set_placeholder¶
Sets the placeholder widget that is shown in the list when it doesn't display any visible children.
Parameters:
placeholder— aGtkWidget
set_selection_mode¶
Sets how selection works in the listbox.
Parameters:
mode— TheGtkSelectionMode
set_show_separators¶
Sets whether the list box should show separators between rows.
Parameters:
show_separators—Trueto show separators
set_sort_func¶
Sets a sort function.
By setting a sort function on the box one can dynamically reorder
the rows of the list, based on the contents of the rows.
The sort_func will be called for each row after the call, and will
continue to be called each time a row changes (via
ListBoxRow.changed) and when ListBox.invalidate_sort
is called.
Note that using a sort function is incompatible with using a model
(see ListBox.bind_model).
Parameters:
sort_func— the sort function
set_tab_behavior¶
Sets the behavior of the <kbd>Tab</kbd> and <kbd>Shift</kbd>+<kbd>Tab</kbd> keys.
Parameters:
behavior— the tab behavior
unselect_all¶
Unselect all children of box, if the selection mode allows it.
unselect_row¶
Unselects a single row of box, if the selection mode allows it.
Parameters:
row— the row to unselect
Properties¶
accept_unpaired_release¶
Whether to accept unpaired release events.
activate_on_single_click¶
Determines whether children can be activated with a single click, or require a double-click.
selection_mode¶
The selection mode used by the list box.
show_separators¶
Whether to show separators between rows.
tab_behavior¶
Behavior of the <kbd>Tab</kbd> key
Signals¶
activate-cursor-row¶
Emitted when the cursor row is activated.
move-cursor¶
Emitted when the user initiates a cursor movement.
The default bindings for this signal come in two variants, the variant with the Shift modifier extends the selection, the variant without the Shift modifier does not. There are too many key combinations to list them all here.
- <kbd>←</kbd>, <kbd>→</kbd>, <kbd>↑</kbd>, <kbd>↓</kbd> move by individual children
- <kbd>Home</kbd>, <kbd>End</kbd> move to the ends of the box
- <kbd>PgUp</kbd>, <kbd>PgDn</kbd> move vertically by pages
row-activated¶
Emitted when a row has been activated by the user.
row-selected¶
Emitted when a new row is selected, or (with a None row)
when the selection is cleared.
When the box is using SelectionMode.MULTIPLE, this signal will not
give you the full picture of selection changes, and you should use
the ListBox.selected-rows-changed signal instead.
select-all¶
Emitted to select all children of the box, if the selection mode permits it.
This is a keybinding signal.
The default binding for this signal is <kbd>Ctrl</kbd>-<kbd>a</kbd>.
selected-rows-changed¶
Emitted when the set of selected rows changes.
toggle-cursor-row¶
Emitted when the cursor row is toggled.
The default bindings for this signal is <kbd>Ctrl</kbd>+<kbd>␣</kbd>.
unselect-all¶
Emitted to unselect all children of the box, if the selection mode permits it.
This is a keybinding signal.
The default binding for this signal is <kbd>Ctrl</kbd>-<kbd>Shift</kbd>-<kbd>a</kbd>.