[pygtk] Confused by multiple selections

Dieter Verfaillie dieterv at optionexplicit.be
Fri Oct 15 17:42:00 WST 2010


Hi,

Quoting Alan <alfraealba at googlemail.com>:
> The problem is that if more than one item is selected, say two, you have
> to click on the second item twice for it to register when the button is
> clicked.

You should look into the 'changed' signal of the treeselection object
instead of using the 'cursor-changed' signal of the treeview itself.
I've adapted your first example, see below.

hth,
Dieter


#!/usr/bin/env python


import pygtk
pygtk.require('2.0')
import gtk
import logging
# Turn logging on or off
logging.basicConfig(level=logging.DEBUG,  
format='%(asctime)s%(levelname)s: %(message)s',datefmt='%Y-%m-%d  
%H:%M:%S') # Turn logging on
#logging.basicConfig() # Turn debugging off


class BasicTreeViewExample:
         pathlist = []

         def __init__(self):
                 # Create a new window
                 self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
                 self.window.set_title("Music Collection")
                 self.window.set_size_request(500, 200)
                 self.window.connect("delete_event", self.on_delete_event)

                 table1 = gtk.Table(1, 2, False)
                 table1.set_row_spacings( 5)
                 table1.set_col_spacings(5)
                 self.window.add(table1)

                 # create the TreeView using liststore
                 self.treeview = self._my_treeview()
                 treeselection = self.treeview.get_selection()
                 treeselection.connect('changed', self.selecttest1)
                 treeselection.set_mode(gtk.SELECTION_MULTIPLE)
                 # values colx, coly, roxx, rowy
                 table1.attach(self.treeview, 0, 1, 0, 1)

                 my_button  = gtk.Button("click here")
                 my_button.connect('clicked', self.on_button_clicked)
                 table1.attach(my_button, 1,2, 0, 1)

                 self.window.show_all()

         def _my_treeview(self):
                 # create a ListStore with one string column to use as  
the model
                 self.liststore = gtk.ListStore(str, str)
                 self._add_data()
                 self.treeview = gtk.TreeView(self.liststore)
                 self.treeview.set_headers_visible(False)
                 # create the TreeViewColumn to display the data
                 self.tvcolumn = gtk.TreeViewColumn()
                 # add tvcolumn to treeview
                 self.treeview.append_column(self.tvcolumn)
                 # create a CellRendererText to render the data
                 self.cell = gtk.CellRendererText()
                 # add the cell to the tvcolumn and allow it to expand
                 self.tvcolumn.pack_start(self.cell, True)
                 # set the cell "text" attribute to column 0 - retrieve text
                 # from that column in treestore
                 self.tvcolumn.add_attribute(self.cell, 'text', 0)
                 # make it searchable
                 self.treeview.set_search_column(0)
                 # Allow drag and drop reordering of rows
                 self.treeview.set_reorderable(False)
                 return (self.treeview)

         def _add_data(self):
                 # we'll add some data now - 4 rows
                 self.liststore.append(["acdc - highway to hell",   
"$MUSIC\acdc\highway"])
                 self.liststore.append(["Saxon - inner sanctum",   
"$MUSIC\saxon\inner"])
                 self.liststore.append(["Gun - Greatest Hist",   
"$MUSIC\gun\greatest_hits"])
                 self.liststore.append(["ABC - Look of love",   
"$music\abc\look_of_love"])

         # close the window and quit
         def on_delete_event(self, widget, event, data=None):
                 gtk.main_quit()
                 return False

         def on_button_clicked(self, widget,  data=None):
                 print ("button pressed")
                 logging.debug("contents of pathlist" + str(self.pathlist))
                 logging.debug("length of pathlist=" + str(len(self.pathlist)))
                 logging.debug(str("value of pathlist[0]=" +  
str(self.pathlist[0])))
                 model = self.treeview.get_model()
                 for x in self.pathlist:
                         iter = model.get_iter(x)
                         if iter != None :
                                 print ("value or select store=" +  
model.get_value(iter, 1))

         def selecttest1(self, treeselection):
                 (model, self.pathlist) = treeselection.get_selected_rows()
                 logging.debug("lenght of pathlist=" + str(len(self.pathlist)))


def main():
         gtk.main()


if __name__ == "__main__":
     tvexample = BasicTreeViewExample()
     main()




----------------------------------------------------------------
This message was sent using IMP, the Internet Messaging Program.


More information about the pygtk mailing list