Gtk.ComboBox

g GObject.GInterface GObject.GInterface Gtk.Accessible Gtk.Accessible GObject.GInterface->Gtk.Accessible Gtk.Buildable Gtk.Buildable GObject.GInterface->Gtk.Buildable Gtk.CellEditable Gtk.CellEditable GObject.GInterface->Gtk.CellEditable Gtk.CellLayout Gtk.CellLayout GObject.GInterface->Gtk.CellLayout Gtk.ConstraintTarget Gtk.ConstraintTarget GObject.GInterface->Gtk.ConstraintTarget GObject.InitiallyUnowned GObject.InitiallyUnowned Gtk.Widget Gtk.Widget GObject.InitiallyUnowned->Gtk.Widget GObject.Object GObject.Object GObject.Object->GObject.InitiallyUnowned Gtk.Accessible->Gtk.Widget Gtk.Buildable->Gtk.Widget Gtk.ComboBox Gtk.ComboBox Gtk.CellEditable->Gtk.ComboBox Gtk.CellLayout->Gtk.ComboBox Gtk.ConstraintTarget->Gtk.Widget Gtk.Widget->Gtk.ComboBox

Example

../_images/ComboBox.png
Subclasses:

Gtk.ComboBoxText

Methods

Inherited:

Gtk.Widget (181), GObject.Object (37), Gtk.Accessible (15), Gtk.Buildable (1), Gtk.CellEditable (3), Gtk.CellLayout (9)

Structs:

Gtk.WidgetClass (18), GObject.ObjectClass (5)

class

new ()

class

new_with_entry ()

class

new_with_model (model)

class

new_with_model_and_entry (model)

get_active ()

get_active_id ()

get_active_iter ()

get_button_sensitivity ()

get_child ()

get_entry_text_column ()

get_has_entry ()

get_id_column ()

get_model ()

get_popup_fixed_width ()

popdown ()

popup ()

popup_for_device (device)

set_active (index_)

set_active_id (active_id)

set_active_iter (iter)

set_button_sensitivity (sensitivity)

set_child (child)

set_entry_text_column (text_column)

set_id_column (id_column)

set_model (model)

set_popup_fixed_width (fixed)

set_row_separator_func (func, *data)

Virtual Methods

Inherited:

Gtk.Widget (25), GObject.Object (7), Gtk.Accessible (6), Gtk.Buildable (9), Gtk.CellEditable (3), Gtk.CellLayout (9)

do_activate ()

do_changed ()

do_format_entry_text (path)

Properties

Inherited:

Gtk.Widget (34), Gtk.Accessible (1), Gtk.CellEditable (1)

Name

Type

Flags

Short Description

active

int

r/w/en

active-id

str

r/w/en

button-sensitivity

Gtk.SensitivityType

r/w/en

child

Gtk.Widget

r/w/en

entry-text-column

int

r/w/en

has-entry

bool

r/w/co

has-frame

bool

r/w/en

id-column

int

r/w/en

model

Gtk.TreeModel

r/w/en

popup-fixed-width

bool

r/w/en

popup-shown

bool

r

Signals

Inherited:

Gtk.Widget (13), GObject.Object (1), Gtk.CellEditable (2)

Name

Short Description

activate

Emitted to when the combo box is activated.

changed

Emitted when the active item is changed.

format-entry-text

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

move-active

Emitted to move the active selection.

popdown

Emitted to popdown the combo box list.

popup

Emitted to popup the combo box list.

Fields

Inherited:

Gtk.Widget (13), GObject.Object (1), Gtk.CellEditable (2)

Name

Type

Access

Description

parent_instance

Gtk.Widget

r

Class Details

class Gtk.ComboBox(**kwargs)
Bases:

Gtk.Widget, Gtk.CellEditable, Gtk.CellLayout

Abstract:

No

Structure:

Gtk.ComboBoxClass

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

An example Gtk.ComboBox

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 [iface`Gtk`.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 [property`Gtk`.ComboBox:has-entry] property allows the GtkComboBox to contain a [class`Gtk`.Entry]. This entry can be accessed by calling [method`Gtk`.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, [class`Gtk`.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 Gtk.AccessibleRole.COMBO_BOX role.

Deprecated since version 4.10: Use [class`Gtk`.DropDown] instead

classmethod new()[source]
Returns:

A new GtkComboBox

Return type:

Gtk.Widget

Creates a new empty GtkComboBox.

Deprecated since version 4.10: Use [class`Gtk`.DropDown]

classmethod new_with_entry()[source]
Returns:

A new GtkComboBox

Return type:

Gtk.Widget

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 [method`Gtk`.ComboBox.set_entry_text_column].

Deprecated since version 4.10: Use [class`Gtk`.DropDown]

classmethod new_with_model(model)[source]
Parameters:

model (Gtk.TreeModel) – a GtkTreeModel

Returns:

A new GtkComboBox

Return type:

Gtk.Widget

Creates a new GtkComboBox with a model.

Deprecated since version 4.10: Use [class`Gtk`.DropDown]

classmethod new_with_model_and_entry(model)[source]
Parameters:

model (Gtk.TreeModel) – A GtkTreeModel

Returns:

A new GtkComboBox

Return type:

Gtk.Widget

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

See also [ctor`Gtk`.ComboBox.new_with_entry].

Deprecated since version 4.10: Use [class`Gtk`.DropDown]

get_active()[source]
Returns:

An integer which is the index of the currently active item, or -1 if there’s no active item

Return type:

int

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 [struct`Gtk`.TreePath] of the active item.

Deprecated since version 4.10: Use [class`Gtk`.DropDown]

get_active_id()[source]
Returns:

the ID of the active row

Return type:

str or None

Returns the ID of the active row of self.

This value is taken from the active row and the column specified by the [property`Gtk`.ComboBox:id-column] property of self (see [method`Gtk`.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 [property`Gtk`.ComboBox:id-column] property of self is not set, or if no row is active, or if the active row has a None ID value, then None is returned.

Deprecated since version 4.10: Use [class`Gtk`.DropDown]

get_active_iter()[source]
Returns:

a Gtk.TreeIter or None if there is no active item

Return type:

Gtk.TreeIter or None

Returns the iter for the current active item, if it exists.

Deprecated since version 4.10: Use [class`Gtk`.DropDown]

get_button_sensitivity()[source]
Returns:

Gtk.SensitivityType.ON if the dropdown button is sensitive when the model is empty, Gtk.SensitivityType.OFF if the button is always insensitive or Gtk.SensitivityType.AUTO if it is only sensitive as long as the model has one item to be selected.

Return type:

Gtk.SensitivityType

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

Deprecated since version 4.10: Use [class`Gtk`.DropDown]

get_child()[source]
Returns:

the child widget of self

Return type:

Gtk.Widget or None

Gets the child widget of self.

Deprecated since version 4.10: Use [class`Gtk`.DropDown]

get_entry_text_column()[source]
Returns:

A column in the data source model of self.

Return type:

int

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

Deprecated since version 4.10: Use [class`Gtk`.DropDown]

get_has_entry()[source]
Returns:

whether there is an entry in self.

Return type:

bool

Returns whether the combo box has an entry.

Deprecated since version 4.10: Use [class`Gtk`.DropDown]

get_id_column()[source]
Returns:

A column in the data source model of self.

Return type:

int

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

Deprecated since version 4.10: Use [class`Gtk`.DropDown]

get_model()[source]
Returns:

A GtkTreeModel which was passed during construction.

Return type:

Gtk.TreeModel or None

Returns the GtkTreeModel of self.

Deprecated since version 4.10: Use [class`Gtk`.DropDown]

get_popup_fixed_width()[source]
Returns:

True if the popup uses a fixed width

Return type:

bool

Gets whether the popup uses a fixed width.

Deprecated since version 4.10: Use [class`Gtk`.DropDown]

popdown()[source]

Hides the menu or dropdown list of self.

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

Deprecated since version 4.10: Use [class`Gtk`.DropDown]

popup()[source]

Pops up the menu or dropdown list of self.

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

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

Deprecated since version 4.10: Use [class`Gtk`.DropDown]

popup_for_device(device)[source]
Parameters:

device (Gdk.Device) – a GdkDevice

Pops up the menu of self.

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.

Deprecated since version 4.10: Use [class`Gtk`.DropDown]

set_active(index_)[source]
Parameters:

index (int) – An index in the model passed during construction, or -1 to have no active item

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

Deprecated since version 4.10: Use [class`Gtk`.DropDown]

set_active_id(active_id)[source]
Parameters:

active_id (str or None) – the ID of the row to select

Returns:

True if a row with a matching ID was found. If a None active_id was given to unset the active row, the function always returns True.

Return type:

bool

Changes the active row of self 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 [property`Gtk`.ComboBox:id-column] property of self is unset or if no row has the given ID then the function does nothing and returns False.

Deprecated since version 4.10: Use [class`Gtk`.DropDown]

set_active_iter(iter)[source]
Parameters:

iter (Gtk.TreeIter or None) – The GtkTreeIter

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

If iter is None, the active item is unset.

Deprecated since version 4.10: Use [class`Gtk`.DropDown]

set_button_sensitivity(sensitivity)[source]
Parameters:

sensitivity (Gtk.SensitivityType) – specify the sensitivity of the dropdown button

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

Deprecated since version 4.10: Use [class`Gtk`.DropDown]

set_child(child)[source]
Parameters:

child (Gtk.Widget or None) – the child widget

Sets the child widget of self.

Deprecated since version 4.10: Use [class`Gtk`.DropDown]

set_entry_text_column(text_column)[source]
Parameters:

text_column (int) – A column in model to get the strings from for the internal entry

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

For this column no separate [class`Gtk`.CellRenderer] is needed.

The column text_column in the model of self must be of type GObject.TYPE_STRING.

This is only relevant if self has been created with [property`Gtk`.ComboBox:has-entry] as True.

Deprecated since version 4.10: Use [class`Gtk`.DropDown]

set_id_column(id_column)[source]
Parameters:

id_column (int) – A column in model to get string IDs for values from

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

The column id_column in the model of self must be of type GObject.TYPE_STRING.

Deprecated since version 4.10: Use [class`Gtk`.DropDown]

set_model(model)[source]
Parameters:

model (Gtk.TreeModel or None) – A GtkTreeModel

Sets the model used by self 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 [method`Gtk`.CellLayout.clear] yourself if you need to set up different cell renderers for the new model.

Deprecated since version 4.10: Use [class`Gtk`.DropDown]

set_popup_fixed_width(fixed)[source]
Parameters:

fixed (bool) – whether to use a fixed popup width

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.

Deprecated since version 4.10: Use [class`Gtk`.DropDown]

set_row_separator_func(func, *data)[source]
Parameters:

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.

Deprecated since version 4.10: Use [class`Gtk`.DropDown]

do_activate() virtual
do_changed() virtual

Signal is emitted when the active item is changed.

do_format_entry_text(path) virtual
Parameters:

path (str) –

Return type:

str

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

Signal Details

Gtk.ComboBox.signals.activate(combo_box)
Signal Name:

activate

Flags:

RUN_FIRST, ACTION

Parameters:

combo_box (Gtk.ComboBox) – The object which received the signal

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.

New in version 4.6.

Gtk.ComboBox.signals.changed(combo_box)
Signal Name:

changed

Flags:

RUN_LAST

Parameters:

combo_box (Gtk.ComboBox) – The object which received the signal

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 [method`Gtk`.ComboBox.set_active_iter]. It will also be emitted while typing into the entry of a combo box with an entry.

Gtk.ComboBox.signals.format_entry_text(combo_box, path)
Signal Name:

format-entry-text

Flags:

RUN_LAST

Parameters:
  • combo_box (Gtk.ComboBox) – The object which received the signal

  • path (str) – the [struct`Gtk`.TreePath] string from the combo box’s current model to format text for

Returns:

a newly allocated string representing path for the current GtkComboBox model.

Return type:

str

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

See [property`Gtk`.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 [property`Gtk`.ComboBox:entry-text-column] model column.

Here’s an example signal handler which fetches data from the model and displays it in the entry. ```c static str * format_entry_text_callback (Gtk.ComboBox *combo, const str *path, object user_data) { Gtk.TreeIter iter; Gtk.TreeModel model; double value;

model = Gtk.ComboBox.get_model (combo);

Gtk.TreeModel.get_iter_from_string (model, &iter, path); Gtk.TreeModel.get (model, &iter, THE_DOUBLE_VALUE_COLUMN, &value, -1);

return g_strdup_printf (“%g”, value); } ```

Gtk.ComboBox.signals.move_active(combo_box, scroll_type)
Signal Name:

move-active

Flags:

RUN_LAST, ACTION

Parameters:

Emitted to move the active selection.

This is an keybinding signal.

Gtk.ComboBox.signals.popdown(combo_box)
Signal Name:

popdown

Flags:

RUN_LAST, ACTION

Parameters:

combo_box (Gtk.ComboBox) – The object which received the signal

Return type:

bool

Emitted to popdown the combo box list.

This is an keybinding signal.

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

Gtk.ComboBox.signals.popup(combo_box)
Signal Name:

popup

Flags:

RUN_LAST, ACTION

Parameters:

combo_box (Gtk.ComboBox) – The object which received the signal

Emitted to popup the combo box list.

This is an keybinding signal.

The default binding for this signal is Alt+Down.

Property Details

Gtk.ComboBox.props.active
Name:

active

Type:

int

Default Value:

-1

Flags:

READABLE, WRITABLE, EXPLICIT_NOTIFY

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 [struct`Gtk`.TreePath] of the active item.

Gtk.ComboBox.props.active_id
Name:

active-id

Type:

str

Default Value:

None

Flags:

READABLE, WRITABLE, EXPLICIT_NOTIFY

The value of the ID column of the active row.

Gtk.ComboBox.props.button_sensitivity
Name:

button-sensitivity

Type:

Gtk.SensitivityType

Default Value:

Gtk.SensitivityType.AUTO

Flags:

READABLE, WRITABLE, EXPLICIT_NOTIFY

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

Gtk.ComboBox.props.child
Name:

child

Type:

Gtk.Widget

Default Value:

None

Flags:

READABLE, WRITABLE, EXPLICIT_NOTIFY

The child widget.

Gtk.ComboBox.props.entry_text_column
Name:

entry-text-column

Type:

int

Default Value:

-1

Flags:

READABLE, WRITABLE, EXPLICIT_NOTIFY

The model column to associate with strings from the entry.

This is property only relevant if the combo was created with [property`Gtk`.ComboBox:has-entry] is True.

Gtk.ComboBox.props.has_entry
Name:

has-entry

Type:

bool

Default Value:

False

Flags:

READABLE, WRITABLE, CONSTRUCT_ONLY

Whether the combo box has an entry.

Gtk.ComboBox.props.has_frame
Name:

has-frame

Type:

bool

Default Value:

True

Flags:

READABLE, WRITABLE, EXPLICIT_NOTIFY

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

Gtk.ComboBox.props.id_column
Name:

id-column

Type:

int

Default Value:

-1

Flags:

READABLE, WRITABLE, EXPLICIT_NOTIFY

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

Gtk.ComboBox.props.model
Name:

model

Type:

Gtk.TreeModel

Default Value:

None

Flags:

READABLE, WRITABLE, EXPLICIT_NOTIFY

The model from which the combo box takes its values.

Gtk.ComboBox.props.popup_fixed_width
Name:

popup-fixed-width

Type:

bool

Default Value:

True

Flags:

READABLE, WRITABLE, EXPLICIT_NOTIFY

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

Gtk.ComboBox.props.popup_shown
Name:

popup-shown

Type:

bool

Default Value:

False

Flags:

READABLE

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.