python 2.7 - PyGtk : destroying combobox causes error -
my aim destroy combobox if 1 of items active.
i wrote test code :
import pygtk pygtk.require('2.0') import gtk import gobject def remove(combobox): if 'optionc' in combobox.get_active_text(): combobox.destroy() window = gtk.window(gtk.window_toplevel) window.set_default_size(800, 600) window.set_title("test") window.connect("destroy", gtk.main_quit) main_box = gtk.vbox(false, 2) window.add(main_box) nb = 3 in range(nb): liststore = gtk.liststore(gobject.type_string) combo = gtk.combobox(liststore) cell = gtk.cellrenderertext() combo.pack_start(cell, true) combo.add_attribute(cell, 'text', 0) text in ["optiona-%d"%(i+1), "optionb-%d"%(i+1), "optionc-%d"%(i+1)]: combo.append_text(text) combo.set_active(0) combo.connect("changed", remove) main_box.pack_start(combo, expand=false) window.show_all() gtk.main()
if open popup of combobox , click select "optionc", have message :
combo.py:29: warning: invalid unclassed pointer in cast `gobject' gtk.main() combo.py:29: warning: g_object_notify: assertion `g_is_object (object)' failed gtk.main() combo.py:29: warning: g_object_set: assertion `g_is_object (object)' failed gtk.main()
but if select "optionc" scolling combobox (without opening popup), no error encountered.
thanks advice!
answer : (working pygtk version 2.24 not 2.16)
replace block :
liststore = gtk.liststore(gobject.type_string) combo = gtk.combobox(liststore) cell = gtk.cellrenderertext() combo.pack_start(cell, true) combo.add_attribute(cell, 'text', 0)
by function :
combo = gtk.combo_box_new_text()
it comes because use liststore. new gtk code should use combo_box_new_text()
here code working :
for in range(nb): combo = gtk.combo_box_new_text() cell = gtk.cellrenderertext() combo.pack_start(cell, true) combo.add_attribute(cell, 'text', 0) text in ["optiona-%d"%(i+1), "optionb-%d"%(i+1), "optionc-%d"%(i+1)]: combo.append_text(text) combo.set_active(0) combo.connect("changed", remove) main_box.pack_start(combo, expand=false) window.show_all() gtk.main()
Comments
Post a Comment