Text in gtk.ComboBox without active item

Posted by Yotam on Stack Overflow See other posts from Stack Overflow or by Yotam
Published on 2010-05-16T21:41:27Z Indexed on 2010/05/16 21:50 UTC
Read the original article Hit count: 167

Filed under:
|
|

The following PyGTk code, gives a combo-box without an active item. This serves a case where we do not want to have a default, and force the user to select.

Still, is there a way to have the empty combo-bar show something like: "Select an item..." without adding a dummy item?

import gtk
import sys
say = sys.stdout.write

def cb_changed(w):
    say("Active index=%d\n" % w.get_active())

topwin = gtk.Window()
topwin.set_title("No Default")
topwin.set_size_request(0x100, 0x20)
topwin.connect('delete-event', gtk.main_quit)
vbox = gtk.VBox()

ls = gtk.ListStore(str, str)
combo = gtk.ComboBox(ls)
cell = gtk.CellRendererText()
combo.pack_start(cell)
combo.add_attribute(cell, 'text', 0)
combo.connect('changed', cb_changed)

ls.clear()
map(lambda i: ls.append(["Item-%d" % i, "Id%d" % i]), range(3))

vbox.pack_start(combo, padding=2)
topwin.add(vbox)
topwin.show_all()

gtk.main()
say("%s Exiting\n" % sys.argv[0])
sys.exit(0)

© Stack Overflow or respective owner

Related posts about pygtk

Related posts about combobox