Hello people!

does anyone know in what way can you edit a default value inside a gtk.ComboBox?

Dani AI

Generated

asked how to edit the default value in a gtk.ComboBox — there are two common, reliable approaches depending on whether you want a free-form entry or to edit the items in the model. 's hint that the combo can expose an Entry is exactly one of those options; the widget can contain an Entry (has-entry) and that Entry can be manipulated directly. (api.pygobject.gnome.org)

For a simple text combo use the convenience text combo (ComboBoxText) or create a ComboBox with an entry, then access the Entry and change its text. Example (PyGObject / GTK3):

from gi.repository import Gtk

combo = Gtk.ComboBoxText.new_with_entry()
combo.append_text("Option A")
combo.set_active(0)

entry = combo.get_child()   # Gtk.Entry
entry.set_text("New default")

The ComboBoxText convenience API and the ability to create a combo with an entry are documented in the GTK API. (docs.gtk.org)

If the dropdown is model-backed and you want edits to change the stored items, make the cell renderer editable and commit edits into the ListStore from the renderer's "edited" signal. The signal carries a path you can use to get the TreeIter and update the model:

def on_edited(cell, path, new_text, store):
    treeiter = store.get_iter(path)
    store.set_value(treeiter, 0, new_text)

store = Gtk.ListStore(str)
store.append(["Alpha"])
combo = Gtk.ComboBox.new_with_model(store)

renderer = Gtk.CellRendererText()
renderer.set_property("editable", True)
renderer.connect("edited", on_edited, store)

combo.pack_start(renderer, True)
combo.add_attribute(renderer, "text", 0)

The CellRendererText::edited signal requires the application to update the model when an edit happens; use the TreeModel/ListStore functions to commit the change. (docs.gtk.org)

If the UI was built in Glade or you're on older PyGTK, use ComboBoxEntry or set the widget's has-entry / text-column properties in the builder — the exact API names differ between PyGTK (GTK2) and PyGObject (GTK3+), so match examples to your binding. (docs.huihoo.com)

Notes: choose the entry approach for "allow arbitrary text", and the editable-renderer approach when edits should update the stored list. Always match code to your GTK version/binding and test the handler to ensure the model is updated.

Recommended Answers

All 3 Replies

I think this should work ...

# get the string
str1 = combo.entry.get_text()
# process the string str1
# then put it back
combo.entry.set_text(str1)

Avner, you seem to get gtk to work, all I ever get is a pango.dll error. So you are way ahead of me!

Avner,
does GTK work pretty well for you?

Yea, but i understand from you that your'e using it on Windows platform.
I'm using it on Linux with glade...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.