Skip to content

Gtk.ComboBox

class — extends Widget, Accessible, Buildable, CellEditable, CellLayout, ConstraintTarget

:::warning Deprecated since 4.10 This API is deprecated. :::

A GtkComboBox is a widget that allows the user to choose from a list of valid choices.

<picture> <source srcset="combo-box-dark.png" media="(prefers-color-scheme: dark)"> <img alt="An example GtkComboBox" src="combo-box.png"> </picture>

The GtkComboBox displays the selected choice; when activated, the GtkComboBox displays a popup which allows the user to make a new choice.

The GtkComboBox uses the model-view pattern; the list of valid choices is specified in the form of a tree model, and the display of the choices can be adapted to the data in the model by using cell renderers, as you would in a tree view. This is possible since GtkComboBox implements the CellLayout interface. The tree model holding the valid choices is not restricted to a flat list, it can be a real tree, and the popup will reflect the tree structure.

To allow the user to enter values not in the model, the ComboBox.has-entry property allows the GtkComboBox to contain a Entry. This entry can be accessed by calling ComboBox.get_child on the combo box.

For a simple list of textual choices, the model-view API of GtkComboBox can be a bit overwhelming. In this case, ComboBoxText offers a simple alternative. Both GtkComboBox and GtkComboBoxText can contain an entry.

CSS nodes

combobox
├── box.linked
│   ╰── button.combo
│       ╰── box
│           ├── cellview
│           ╰── arrow
╰── window.popup

A normal combobox contains a box with the .linked class, a button with the .combo class and inside those buttons, there are a cellview and an arrow.

combobox
├── box.linked
│   ├── entry.combo
│   ╰── button.combo
│       ╰── box
│           ╰── arrow
╰── window.popup

A GtkComboBox with an entry has a single CSS node with name combobox. It contains a box with the .linked class. That box contains an entry and a button, both with the .combo class added. The button also contains another node with name arrow.

Accessibility

GtkComboBox uses the AccessibleRole.combo_box role.

Constructors

new

@classmethod
def new(cls) -> Widget

:::warning Deprecated since 4.10 This API is deprecated. :::

Creates a new empty GtkComboBox.

new_with_entry

@classmethod
def new_with_entry(cls) -> Widget

:::warning Deprecated since 4.10 This API is deprecated. :::

Creates a new empty GtkComboBox with an entry.

In order to use a combo box with entry, you need to tell it which column of the model contains the text for the entry by calling ComboBox.set_entry_text_column.

new_with_model

@classmethod
def new_with_model(cls, model: TreeModel) -> Widget

:::warning Deprecated since 4.10 This API is deprecated. :::

Creates a new GtkComboBox with a model.

Parameters:

  • model — a GtkTreeModel

new_with_model_and_entry

@classmethod
def new_with_model_and_entry(cls, model: TreeModel) -> Widget

:::warning Deprecated since 4.10 This API is deprecated. :::

Creates a new empty GtkComboBox with an entry and a model.

See also ComboBox.new_with_entry.

Parameters:

  • model — A GtkTreeModel

Methods

get_active

def get_active(self) -> int

:::warning Deprecated since 4.10 This API is deprecated. :::

Returns the index of the currently active item.

If the model is a non-flat treemodel, and the active item is not an immediate child of the root of the tree, this function returns gtk_tree_path_get_indices (path)[0], where path is the TreePath of the active item.

get_active_id

def get_active_id(self) -> str | None

:::warning Deprecated since 4.10 This API is deprecated. :::

Returns the ID of the active row of combo_box.

This value is taken from the active row and the column specified by the ComboBox.id-column property of combo_box (see ComboBox.set_id_column).

The returned value is an interned string which means that you can compare the pointer by value to other interned strings and that you must not free it.

If the ComboBox.id-column property of combo_box is not set, or if no row is active, or if the active row has a None ID value, then None is returned.

get_active_iter

def get_active_iter(self) -> tuple[bool, TreeIter]

:::warning Deprecated since 4.10 This API is deprecated. :::

Sets iter to point to the currently active item.

If no item is active, iter is left unchanged.

get_button_sensitivity

def get_button_sensitivity(self) -> SensitivityType

:::warning Deprecated since 4.10 This API is deprecated. :::

Returns whether the combo box sets the dropdown button sensitive or not when there are no items in the model.

get_child

def get_child(self) -> Widget | None

:::warning Deprecated since 4.10 This API is deprecated. :::

Gets the child widget of combo_box.

get_entry_text_column

def get_entry_text_column(self) -> int

:::warning Deprecated since 4.10 This API is deprecated. :::

Returns the column which combo_box is using to get the strings from to display in the internal entry.

get_has_entry

def get_has_entry(self) -> bool

:::warning Deprecated since 4.10 This API is deprecated. :::

Returns whether the combo box has an entry.

get_id_column

def get_id_column(self) -> int

:::warning Deprecated since 4.10 This API is deprecated. :::

Returns the column which combo_box is using to get string IDs for values from.

get_model

def get_model(self) -> TreeModel | None

:::warning Deprecated since 4.10 This API is deprecated. :::

Returns the GtkTreeModel of combo_box.

get_popup_fixed_width

def get_popup_fixed_width(self) -> bool

:::warning Deprecated since 4.10 This API is deprecated. :::

Gets whether the popup uses a fixed width.

popdown

def popdown(self) -> None

:::warning Deprecated since 4.10 This API is deprecated. :::

Hides the menu or dropdown list of combo_box.

This function is mostly intended for use by accessibility technologies; applications should have little use for it.

popup

def popup(self) -> None

:::warning Deprecated since 4.10 This API is deprecated. :::

Pops up the menu or dropdown list of combo_box.

This function is mostly intended for use by accessibility technologies; applications should have little use for it.

Before calling this, combo_box must be mapped, or nothing will happen.

popup_for_device

def popup_for_device(self, device: Gdk.Device) -> None

:::warning Deprecated since 4.10 This API is deprecated. :::

Pops up the menu of combo_box.

Note that currently this does not do anything with the device, as it was previously only used for list-mode combo boxes, and those were removed in GTK 4. However, it is retained in case similar functionality is added back later.

Parameters:

  • device — a GdkDevice

set_active

def set_active(self, index_: int) -> None

:::warning Deprecated since 4.10 This API is deprecated. :::

Sets the active item of combo_box to be the item at index.

Parameters:

  • index_ — An index in the model passed during construction, or -1 to have no active item

set_active_id

def set_active_id(self, active_id: str | None = ...) -> bool

:::warning Deprecated since 4.10 This API is deprecated. :::

Changes the active row of combo_box to the one that has an ID equal to active_id.

If active_id is None, the active row is unset. Rows having a None ID string cannot be made active by this function.

If the ComboBox.id-column property of combo_box is unset or if no row has the given ID then the function does nothing and returns False.

Parameters:

  • active_id — the ID of the row to select

set_active_iter

def set_active_iter(self, iter: TreeIter | None = ...) -> None

:::warning Deprecated since 4.10 This API is deprecated. :::

Sets the current active item to be the one referenced by iter.

If iter is None, the active item is unset.

Parameters:

  • iter — The GtkTreeIter

set_button_sensitivity

def set_button_sensitivity(self, sensitivity: SensitivityType | int) -> None

:::warning Deprecated since 4.10 This API is deprecated. :::

Sets whether the dropdown button of the combo box should update its sensitivity depending on the model contents.

Parameters:

  • sensitivity — specify the sensitivity of the dropdown button

set_child

def set_child(self, child: Widget | None = ...) -> None

:::warning Deprecated since 4.10 This API is deprecated. :::

Sets the child widget of combo_box.

Parameters:

  • child — the child widget

set_entry_text_column

def set_entry_text_column(self, text_column: int) -> None

:::warning Deprecated since 4.10 This API is deprecated. :::

Sets the model column which combo_box should use to get strings from to be text_column.

For this column no separate CellRenderer is needed.

The column text_column in the model of combo_box must be of type G_TYPE_STRING.

This is only relevant if combo_box has been created with ComboBox.has-entry as True.

Parameters:

  • text_column — A column in model to get the strings from for the internal entry

set_id_column

def set_id_column(self, id_column: int) -> None

:::warning Deprecated since 4.10 This API is deprecated. :::

Sets the model column which combo_box should use to get string IDs for values from.

The column id_column in the model of combo_box must be of type G_TYPE_STRING.

Parameters:

  • id_column — A column in model to get string IDs for values from

set_model

def set_model(self, model: TreeModel | None = ...) -> None

:::warning Deprecated since 4.10 This API is deprecated. :::

Sets the model used by combo_box to be model.

Will unset a previously set model (if applicable). If model is None, then it will unset the model.

Note that this function does not clear the cell renderers, you have to call CellLayout.clear yourself if you need to set up different cell renderers for the new model.

Parameters:

  • model — A GtkTreeModel

set_popup_fixed_width

def set_popup_fixed_width(self, fixed: bool) -> None

:::warning Deprecated since 4.10 This API is deprecated. :::

Specifies whether the popup’s width should be a fixed width.

If fixed is True, the popup's width is set to match the allocated width of the combo box.

Parameters:

  • fixed — whether to use a fixed popup width

set_row_separator_func

def set_row_separator_func(self, func: TreeViewRowSeparatorFunc | None = ...) -> None

:::warning Deprecated since 4.10 This API is deprecated. :::

Sets the row separator function, which is used to determine whether a row should be drawn as a separator.

If the row separator function is None, no separators are drawn. This is the default value.

Parameters:

  • func — a GtkTreeViewRowSeparatorFunc

Virtual methods

do_activate

def do_activate(self) -> None

do_changed

def do_changed(self) -> None

Signal is emitted when the active item is changed.

do_format_entry_text

def do_format_entry_text(self, path: str) -> str

Signal which allows you to change how the text displayed in a combo box’s entry is displayed.

Properties

active

active: int  # read/write

The item which is currently active.

If the model is a non-flat treemodel, and the active item is not an immediate child of the root of the tree, this property has the value gtk_tree_path_get_indices (path)[0], where path is the TreePath of the active item.

active_id

active_id: str  # read/write

The value of the ID column of the active row.

button_sensitivity

button_sensitivity: SensitivityType | int  # read/write

Whether the dropdown button is sensitive when the model is empty.

child

child: Widget  # read/write

The child widget.

entry_text_column

entry_text_column: int  # read/write

The model column to associate with strings from the entry.

This is property only relevant if the combo was created with ComboBox.has-entry is True.

has_entry

has_entry: bool  # read/write

Whether the combo box has an entry.

has_frame

has_frame: bool  # read/write

The has-frame property controls whether a frame is drawn around the entry.

id_column

id_column: int  # read/write

The model column that provides string IDs for the values in the model, if != -1.

model

model: TreeModel  # read/write

The model from which the combo box takes its values.

popup_fixed_width

popup_fixed_width: bool  # read/write

Whether the popup's width should be a fixed width matching the allocated width of the combo box.

popup_shown

popup_shown: bool  # read-only

Whether the combo boxes dropdown is popped up.

Note that this property is mainly useful, because it allows you to connect to notify::popup-shown.

Signals

activate

def on_activate(self) -> None: ...

Emitted to when the combo box is activated.

The ::activate signal on GtkComboBox is an action signal and emitting it causes the combo box to pop up its dropdown.

changed

def on_changed(self) -> None: ...

Emitted when the active item is changed.

The can be due to the user selecting a different item from the list, or due to a call to ComboBox.set_active_iter. It will also be emitted while typing into the entry of a combo box with an entry.

format-entry-text

def on_format_entry_text(self, path: str) -> str: ...

Emitted to allow changing how the text in a combo box's entry is displayed.

See ComboBox.has-entry.

Connect a signal handler which returns an allocated string representing path. That string will then be used to set the text in the combo box's entry. The default signal handler uses the text from the ComboBox.entry-text-column model column.

Here's an example signal handler which fetches data from the model and displays it in the entry.

static char *
format_entry_text_callback (GtkComboBox *combo,
                            const char *path,
                            gpointer     user_data)
{
  GtkTreeIter iter;
  GtkTreeModel model;
  double       value;

  model = gtk_combo_box_get_model (combo);

  gtk_tree_model_get_iter_from_string (model, &iter, path);
  gtk_tree_model_get (model, &iter,
                      THE_DOUBLE_VALUE_COLUMN, &value,
                      -1);

  return g_strdup_printf ("%g", value);
}

move-active

def on_move_active(self, scroll_type: ScrollType) -> None: ...

Emitted to move the active selection.

This is an keybinding signal.

popdown

def on_popdown(self) -> bool: ...

Emitted to popdown the combo box list.

This is an keybinding signal.

The default bindings for this signal are Alt+Up and Escape.

popup

def on_popup(self) -> None: ...

Emitted to popup the combo box list.

This is an keybinding signal.

The default binding for this signal is Alt+Down.