Gtk.DropDown¶
class — extends Widget, Accessible, Buildable, ConstraintTarget
Allows the user to choose an item from a list of options.
<picture> <source srcset="drop-down-dark.png" media="(prefers-color-scheme: dark)"> <img alt="An example GtkDropDown" src="drop-down.png"> </picture>
The GtkDropDown displays the [selected]DropDown.selected
choice.
The options are given to GtkDropDown in the form of GListModel
and how the individual options are represented is determined by
a ListItemFactory. The default factory displays simple strings,
and adds a checkmark to the selected item in the popup.
To set your own factory, use DropDown.set_factory. It is
possible to use a separate factory for the items in the popup, with
DropDown.set_list_factory.
GtkDropDown knows how to obtain strings from the items in a
StringList; for other models, you have to provide an expression
to find the strings via DropDown.set_expression.
GtkDropDown can optionally allow search in the popup, which is
useful if the list of options is long. To enable the search entry,
use DropDown.set_enable_search.
Here is a UI definition example for GtkDropDown with a simple model:
<object class="GtkDropDown">
<property name="model">
<object class="GtkStringList">
<items>
<item translatable="yes">Factory</item>
<item translatable="yes">Home</item>
<item translatable="yes">Subway</item>
</items>
</object>
</property>
</object>
If a GtkDropDown is created in this manner, or with
DropDown.new_from_strings, for instance, the object returned from
DropDown.get_selected_item will be a StringObject.
To learn more about the list widget framework, see the overview.
CSS nodes¶
GtkDropDown has a single CSS node with name dropdown,
with the button and popover nodes as children.
Accessibility¶
GtkDropDown uses the AccessibleRole.combo_box role.
Constructors¶
new¶
@classmethod
def new(cls, model: Gio.ListModel | None = ..., expression: Expression | None = ...) -> Widget
Creates a new GtkDropDown.
You may want to call DropDown.set_factory
to set up a way to map its items to widgets.
Parameters:
model— the model to useexpression— the expression to use
new_from_strings¶
Creates a new GtkDropDown that is populated with
the strings.
Parameters:
strings— The strings to put in the dropdown
Methods¶
get_enable_search¶
Returns whether search is enabled.
get_expression¶
Gets the expression set that is used to obtain strings from items.
get_factory¶
Gets the factory that's currently used to populate list items.
The factory returned by this function is always used for the
item in the button. It is also used for items in the popup
if DropDown.list-factory is not set.
get_header_factory¶
Gets the factory that's currently used to create header widgets for the popup.
get_list_factory¶
Gets the factory that's currently used to populate list items in the popup.
get_model¶
Gets the model that provides the displayed items.
get_search_match_mode¶
Returns the match mode that the search filter is using.
get_selected¶
Gets the position of the selected item.
get_selected_item¶
Gets the selected item. If no item is selected, None is returned.
get_show_arrow¶
Returns whether to show an arrow within the widget.
set_enable_search¶
Sets whether a search entry will be shown in the popup that allows to search for items in the list.
Note that DropDown.expression must be set for
search to work.
Parameters:
enable_search— whether to enable search
set_expression¶
Sets the expression that gets evaluated to obtain strings from items.
This is used for search in the popup. The expression must have
a value type of G_TYPE_STRING.
Parameters:
expression— aGtkExpression
set_factory¶
Sets the GtkListItemFactory to use for populating list items.
Parameters:
factory— the factory to use
set_header_factory¶
Sets the GtkListItemFactory to use for creating header widgets for the popup.
Parameters:
factory— the factory to use
set_list_factory¶
Sets the GtkListItemFactory to use for populating list items in the popup.
Parameters:
factory— the factory to use
set_model¶
Sets the GListModel to use.
Parameters:
model— the model to use
set_search_match_mode¶
Sets the match mode for the search filter.
Parameters:
search_match_mode— the new match mode
set_selected¶
Selects the item at the given position.
Parameters:
position— the position of the item to select, orINVALID_LIST_POSITION
set_show_arrow¶
Sets whether an arrow will be displayed within the widget.
Parameters:
show_arrow— whether to show an arrow within the widget
Properties¶
enable_search¶
Whether to show a search entry in the popup.
Note that search requires DropDown.expression
to be set.
expression¶
An expression to evaluate to obtain strings to match against the search term.
See DropDown.enable-search for how to enable search.
If DropDown.factory is not set, the expression is also
used to bind strings to labels produced by a default factory.
factory¶
Factory for populating list items.
header_factory¶
The factory for creating header widgets for the popup.
list_factory¶
The factory for populating list items in the popup.
If this is not set, DropDown.factory is used.
model¶
Model for the displayed items.
search_match_mode¶
The match mode for the search filter.
selected¶
The position of the selected item.
If no item is selected, the property has the value
INVALID_LIST_POSITION.
selected_item¶
The selected item.
show_arrow¶
Whether to show an arrow within the GtkDropDown widget.
Signals¶
activate¶
Emitted to when the drop down is activated.
The ::activate signal on GtkDropDown is an action signal and
emitting it causes the drop down to pop up its dropdown.