Hi.
I've been using the pygtk2 stuff, and I'm stuck on how to
handle selecting from a listbox where the selection is
gtk.SELECTION_MULTIPLE. I'm trying to use the
selected_foreach() function, but at this point I'm doing
something wrong. The callback function is passed a
tuple of the rows that are selected, but I was expecting
something more like the tuple returned from a
get_selected() call. If the selection is gtk.SELECTION_SINGLE,
then the get_selected() call works fine (as is documented
in the GTK docs), and a get_value() call can retrieve
the selected line. In my code I've commented out the lines
handling this case, and in its place used the selected_foreach()
call, which I'm trying to learn how to use. Running the code
below you'll see the printout of the selected lines.
I'm probably overlooking something obvious, and would
appreciate a pointer or two. Thanks in advance.
Art Haas
================================================
#
# pygtk test to learn about tree & list widgets
#
import gtk
import gobject
COLUMN_NUMBER = 0
COLUMN_STRING = 1
data = [
[ 1, 'first row' ],
[ 2, 'second row' ],
[ 3, 'third row' ],
[ 4, 'fourth row' ],
[ 5, 'fifth row' ],
[ 6, 'sixth row' ],
[ 7, 'seventh row' ],
[ 8, 'eigth row' ],
[ 9, 'ninth row' ],
[ 10, 'tenth row' ]
]
def list_selections(param):
print param
def selection_cb(selection):
print selection
# tv = selection.get_tree_view()
# print tv
# model = tv.get_model()
# print model
selection.selected_foreach(list_selections)
# sel = selection.get_selected()
# if sel:
# model, iter = sel
# print model
# print iter
# number = model.get_value(iter,COLUMN_NUMBER)
# string = model.get_value(iter,COLUMN_STRING)
# print "Selected #%d '%s'" % (number,string)
def main():
win = gtk.Window()
win.set_title("Main Window")
win.connect("destroy", lambda win: gtk.main_quit())
vbox = gtk.VBox()
win.add(vbox)
vbox.show()
label = gtk.Label("This is a label")
vbox.pack_start(label, expand = gtk.FALSE)
label.show()
sw = gtk.ScrolledWindow(None, None)
sw.set_policy(gtk.POLICY_NEVER, gtk.POLICY_AUTOMATIC)
sw.set_shadow_type(gtk.SHADOW_ETCHED_IN)
vbox.pack_start(sw, expand = gtk.TRUE)
ls = gtk.ListStore(gobject.TYPE_UINT, gobject.TYPE_STRING)
for item in data:
iter = ls.append()
ls.set(iter, COLUMN_NUMBER, item[0],
COLUMN_STRING, item[1])
tv = gtk.TreeView(ls)
tv.set_rules_hint(gtk.TRUE)
tv.set_search_column(COLUMN_STRING)
selection = tv.get_selection()
selection.set_mode(gtk.SELECTION_MULTIPLE)
selection.connect("changed", selection_cb)
sw.add(tv)
tv.show()
renderer = gtk.CellRendererText()
col = gtk.TreeViewColumn('Number', renderer, text=COLUMN_NUMBER)
col.set_sort_column_id(COLUMN_NUMBER)
tv.append_column(col)
renderer = gtk.CellRendererText()
col = gtk.TreeViewColumn('String', renderer, text=COLUMN_STRING)
col.set_sort_column_id(COLUMN_STRING)
tv.append_column(col)
win.set_default_size (200,300)
sw.show()
win.show_all()
gtk.main()
if __name__ == '__main__':
main()
===================================================
--
They that can give up essential liberty to obtain a little temporary
safety deserve neither liberty nor safety.
-- Benjamin Franklin, Historical Review of Pennsylvania, 1759
_______________________________________________
pygtk mailing list [EMAIL PROTECTED]
http://www.daa.com.au/mailman/listinfo/pygtk
Read the PyGTK FAQ: http://www.async.com.br/faq/pygtk/