FAQ Index - Search - Recent Changes - Everything - Add entry

<< Previous Entry | FAQ Entry 16.7 | Next Entry >>

16.7. How do I get the text from the selected item in GtkComboBox/ComboBoxEntry

For the ComboBox it's a matter of getting the value relative to the active iter:

  def on_combo_box_changed(widget):
     model = widget.get_model()
     iter = widget.get_active_iter()
     print model.get_value(iter, 0)

  combo.connect("changed", on_combo_box_changed)
In the example above, when an item is selected, its text is printed out.

For a ComboBoxEntry, the text can be retrieved using:

  combo_box_entry.child.get_text()
Since the Entry text can be changed by the user the "changed" signal is not sufficient to detect whether the user is done changing the Entry. Also the "changed" callback must be changed to:

  def on_combo_box_entry_changed(widget):
     model = widget.get_model()
     iter = widget.get_active_iter()
     if iter:
         print model.get_value(iter, 0)
because the "changed" signal will be issued if a user edits the text (or pastes it from a clipboard) thereby causing no model row to be active. To detect user edits you have to connect to the child Entry.

PyGTK FAQ Wizard | PyGTK Homepage | Feedback to faq at pygtk.org