Gtk.ComboBoxText
Gtk.ComboBoxText — A simple, text-only combo box

Object Hierarchy:
GObject
╰── GInitiallyUnowned
╰── Gtk.Widget
╰── Gtk.Container
╰── Gtk.Bin
╰── Gtk.ComboBox
╰── Gtk.ComboBoxText
See also:
Functions:
- new
() -> Gtk.Widget - new_with_entry
() -> Gtk.Widget - append
(self, id:str=None, text:str) - prepend
(self, id:str=None, text:str) - insert
(self, position:int, id:str=None, text:str) - append_text
(self, text:str) - prepend_text
(self, text:str) - insert_text
(self, position:int, text:str) - remove
(self, position:int) - remove_all
(self) - get_active_text
(self) -> str
Description:
A Gtk.ComboBoxText is a simple variant of GtkComboBox that hides the model-view complexity for simple text-only use cases.
To create a Gtk.ComboBoxText, use Gtk.ComboBoxText::new() or Gtk.ComboBoxText::new_with_entry().
You can add items to a Gtk.ComboBoxText with Gtk.ComboBoxText::append_text(), Gtk.ComboBoxText::insert_text()
or Gtk.ComboBoxText::prepend_text() and remove options with Gtk.ComboBoxText::remove().
If the Gtk.ComboBoxText contains an entry (via the has-entry property), its contents can be retrieved using Gtk.ComboBoxText::get_active_text().
The entry itself can be accessed by calling Gtk.Bin::get_child() on the combo box.
You should not call Gtk.ComboBox::set_model() or attempt to pack more cells into this combo box via its Gtk.CellLayout interface.
Gtk.ComboBoxText as GtkBuildable
The Gtk.ComboBoxText implementation of the GtkBuildable interface supports adding items directly using the <items> element and specifying <item> elements for each item. Each <item> element can specify the id corresponding to the appended text and also supports the regular translation attributes translatable, context and comments.
Here is a UI definition fragment specifying Gtk.ComboBoxText items:
<object class="GtkComboBoxText">
<items>
<item translatable="yes" id="factory">Factory</item>
<item translatable="yes" id="home">Home</item>
<item translatable="yes" id="subway">Subway</item>
</items>
</object>
Function Details:
new()
new () -> Gtk.Widget
Creates a new `Gtk.ComboBoxText, which is aGtk.ComboBox` just displayingstrings.
Returns: A new Gtk.ComboBoxText
Since: 2.24
new_with_entry()
new_with_entry () -> Gtk.Widget
Creates a new `Gtk.ComboBoxText, which is aGtk.ComboBox` just displayingstrings. The combo box created by this function has an entry.
Returns: a new Gtk.ComboBoxText
Since: 2.24
append()
append (self, id:str=None, text:str)
Appends text to the list of strings stored in combo_box.If id is non-None then it is used as the ID of the row.
This is the same as calling Gtk.ComboBoxText:insert() with aposition of -1.
- Since: 2.24
prepend()
prepend (self, id:str=None, text:str)
Prepends text to the list of strings stored in combo_box.If id is non-None then it is used as the ID of the row.
This is the same as calling Gtk.ComboBoxText:insert() with aposition of 0.
- Since: 2.24
insert()
insert (self, position:int, id:str=None, text:str)
Inserts text at position in the list of strings stored in combo_box.If id is non-None then it is used as the ID of the row. See“id-column”.
If position is negative then text is appended.
- Since: 3.0
append_text()
append_text (self, text:str)
Appends text to the list of strings stored in combo_box.
This is the same as calling Gtk.ComboBoxText:insert_text() with aposition of -1.
- Since: 2.24
prepend_text()
prepend_text (self, text:str)
Prepends text to the list of strings stored in combo_box.
This is the same as calling Gtk.ComboBoxText:insert_text() with aposition of 0.
- Since: 2.24
insert_text()
insert_text (self, position:int, text:str)
Inserts text at position in the list of strings stored in combo_box.
If position is negative then text is appended.
This is the same as calling Gtk.ComboBoxText:insert() with a NoneID string.
- Since: 2.24
remove()
remove (self, position:int)
Removes the string at position from combo_box.
- Since: 2.24
remove_all()
remove_all (self)
Removes all the text entries from the combo box.
- Since: 3.0
get_active_text()
get_active_text (self) -> str
Returns the currently active string in combo_box, or Noneif none is selected. If combo_box contains an entry, thisfunction will return its contents (which will not necessarilybe an item from the list).
Returns: a newly allocated string containing thecurrently active text. Must be freed with
g_free().Since: 2.24
Example:
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
class ComboBoxText(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self)
self.set_title("ComboBoxText")
self.set_default_size(150, -1)
self.connect("destroy", Gtk.main_quit)
comboboxtext = Gtk.ComboBoxText()
comboboxtext.append("gnome", "GNOME")
comboboxtext.append("kde", "KDE")
comboboxtext.append("xfce", "XFCE")
comboboxtext.append("lxde", "LXDE")
comboboxtext.set_active_id("xfce")
comboboxtext.connect("changed", self.on_comboboxtext_changed)
self.add(comboboxtext)
def on_comboboxtext_changed(self, comboboxtext):
print("ComboBox selected item: %s" % (comboboxtext.get_active_text()))
window = ComboBoxText()
window.show_all()
Gtk.main()