C.3. Example

The list.py example program that will print out the changes of the selection of a gtklist, and lets you "arrest" list items into a prison by selecting them with the rightmost mouse button. Figure C.1 illustrates the program in action:

Figure C.1 GtkList Example

The source code for list.py is:
 
    1   #!/usr/bin/env python
    2   
    3   # example list.py
    4   
    5   
    6   import gtk, GDK
    7   
    8   class ListExample:
    9       # This is our data identification string to store
   10       # data in list items
   11       list_item_data_key="list_item_data"
   12   
   13       # Main function to set up the user interface
   14       def __init__(self):
   15           # Create a window to put all the widgets in
   16           # connect main_quit() to the "destroy" event of
   17           # the window to handle window manager close-window-events
   18           window = gtk.GtkWindow(gtk.WINDOW_TOPLEVEL)
   19           window.set_title("GtkList Example")
   20           window.connect("destroy", gtk.mainquit)
   21   
   22           # Inside the window we need a box to arrange the widgets
   23           # vertically
   24           vbox = gtk.GtkVBox(gtk.FALSE, 5)
   25           vbox.set_border_width(5)
   26           window.add(vbox)
   27           vbox.show()
   28   
   29           # This is the scrolled window to put the List widget inside
   30           scrolled_window = gtk.GtkScrolledWindow()
   31           scrolled_window.set_usize(250, 150)
   32           vbox.add(scrolled_window)
   33           scrolled_window.show()
   34   
   35           # Create the GtkList widget.
   36           # Connect the sigh_print_selection() signal handler
   37           # function to the "selection_changed" signal of the List
   38           # to print out the selected items each time the selection
   39           # has changed
   40           gtklist = gtk.GtkList()
   41           scrolled_window.add_with_viewport(gtklist)
   42           gtklist.show()
   43           gtklist.connect("selection_changed", self.sigh_print_selection)
   44       
   45           # We create a "Prison" to put a list item in )
   46           frame = gtk.GtkFrame("Prison")
   47           frame.set_usize(200, 50)
   48           frame.set_border_width(5)
   49           frame.set_shadow_type(gtk.SHADOW_OUT)
   50           vbox.add(frame)
   51           frame.show()
   52   
   53           # Connect the sigh_button_event() signal handler to the List
   54           # which will handle the "arresting" of list items
   55           gtklist.connect("button_release_event", self.sigh_button_event, frame)
   56   
   57           # Create a separator
   58           separator = gtk.GtkHSeparator()
   59           vbox.add(separator)
   60           separator.show()
   61   
   62           # Finally create a button and connect its "clicked" signal
   63           # to the destruction of the window
   64           button = gtk.GtkButton("Close")
   65           vbox.add(button)
   66           button.show()
   67           button.connect_object("clicked", window.destroy, window)
   68   
   69           # Now we create 5 list items, each having its own
   70           # label and add them to the List using add()
   71           # Also we query the text string from the label and
   72           # associate it with the list_item_data_key for each list item
   73   
   74           for i in range(5):
   75               buffer = "ListItemContainer with Label #%d" % i
   76               label = gtk.GtkLabel(buffer)
   77               list_item = gtk.GtkListItem()
   78               list_item.add(label)
   79               label.show()
   80               gtklist.add(list_item)
   81               list_item.show()
   82               string = label.get()
   83               list_item.set_data(self.list_item_data_key, string)
   84   
   85           # Here, we are creating another 5 labels, this time
   86           # we use GtkListItem() for the creation
   87           # For adding of the list items we put them all into a
   88           # list, and then add them by a single call to
   89           # append_items().
   90   
   91           dlist = []
   92           for i in range(5, 10):
   93               buffer = "List Item with Label %d" % i
   94               list_item = gtk.GtkListItem(buffer)
   95               dlist.append(list_item)
   96               list_item.show()
   97               list_item.set_data(self.list_item_data_key,
   98                                  list_item.children()[0].get())
   99   
  100           gtklist.append_items(dlist)
  101   
  102           # Finally we want to see the window, don't we? )
  103           window.show()
  104   
  105       # This is the signal handler that got connected to button
  106       # press/release events of the List
  107       def sigh_button_event(self, gtklist, event, frame):
  108           # We only do something if the third (rightmost mouse button
  109           # was released
  110           if event.type == GDK.BUTTON_RELEASE and event.button == 3:
  111               # Fetch the currently selected list item which
  112               # will be our next prisoner )
  113           
  114               dlist = gtklist.get_selection()
  115               if dlist:
  116                   new_prisoner = dlist[0]
  117               else:
  118                   new_prisoner = None
  119   
  120               # Look for already imprisoned list items, we
  121               # will put them back into the list.
  122               dlist = frame.children()
  123               for list_item in dlist:
  124                   list_item.reparent(gtklist)
  125   
  126               # If we have a new prisoner, remove him from the
  127               # List and put him into the frame "Prison".
  128               # We need to unselect the item first.
  129               if new_prisoner:
  130                   static_dlist = [new_prisoner]
  131       
  132                   gtklist.unselect_child(new_prisoner)
  133                   new_prisoner.reparent(frame)
  134   
  135       # This is the signal handler that gets called if List
  136       # emits the "selection_changed" signal
  137       def sigh_print_selection(self, gtklist, func_data=None):
  138           # Fetch the list of selected items
  139           # of the List
  140           dlist = gtklist.get_selection()
  141       
  142           # If there are no selected items there is nothing more
  143           # to do than just telling the user so
  144           if not dlist:
  145               print "Selection cleared"
  146               return
  147   
  148           # Ok, we got a selection and so we print it
  149           str = "The selection is a "
  150   
  151           # Get the list item from the list
  152           # and then query the data associated with list_item_data_key.
  153           # We then just print it
  154           for list_item in dlist:
  155               item_data_string = list_item.get_data(self.list_item_data_key)
  156               str = str + "%s " % item_data_string
  157   
  158           print "%s\n" % str
  159   
  160   def main():
  161       # Fire up the main event loop of gtk
  162       gtk.mainloop()
  163       # We get here after gtk_main_quit() has been called which
  164       # happens if the main window gets destroyed
  165       return 0
  166   
  167   if __name__ == "__main__":
  168       ListExample()
  169       main()