1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| import gtk
# Main window
win = gtk.Window()
win.connect('destroy', lambda w: gtk.main_quit())
win.set_default_size(300, 200)
# Treestore
ts = gtk.TreeStore(str)
iterA = ts.append(None, ["AAA"])
ts.append(iterA, ["A111"])
ts.append(iterA, ["A222"])
ts.append(iterA, ["A333"])
iterB = ts.append(None, ["BBB"])
ts.append(iterB, ["B111"])
ts.append(iterB, ["B222"])
ts.append(iterB, ["B333"])
iterC = ts.append(None, ["CCC"])
ts.append(iterC, ["C111"])
ts.append(iterC, ["C222"])
ts.append(iterC, ["C333"])
# Combobox
combo = gtk.ComboBox(ts)
cell = gtk.CellRendererText()
combo.pack_start(cell)
combo.add_attribute(cell, 'text', 0)
combo.set_active(0)
# Display
al = gtk.Alignment(xalign=0.5, yalign=0.5)
al.add(combo)
win.add(al)
win.show_all()
# Main loop
gtk.main()
# Print last selected before exit
last_iter = combo.get_active_iter()
print ts.get_value(last_iter, 0) |
Partager