On Sun, Jul 27, 2003 at 03:05:47AM -0700, Colin Fox wrote:

> Is there any documentation or examples that show how to use the new
> GtkTree code in python? Especially with a libglade generated interface?
> 
> A working tree example would be ideal. 

There's a tutorial here:

http://liw.iki.fi/liw/texts/gtktreeview-tutorial.html

Don't forget the examples that come with the pygtk distribution.  I whipped
up my own example of a multi-column treeview.  It demonstrates a few things:

* How make certain cells editable.  Here I use two "control" columns
  at the end of my row (store column 2 & 3) that control whether a cell
  is editable and visible.  How the renderer uses these columns is set with
  
    column = gtk.TreeViewColumn(column_titles[i], cell, text=i,
                                editable=ncols+i, visible=ncols+i)

  which means that the renderer will get the text to render from column i
  and determine whether the cell is editable and visible using the 
  values in column ncols+i.  See the properties for the different
  renderers to see what other keywords you can use.
  
* A callback for the "edited" signal which actually sets the new text
  that you type in a cell.  I use set_data/get_data to retrieve the 
  actual store column that the renderer corresponds to (you could also
  pass it as user_data when connecting the callback).
  
* How to get a selection, this is in the on_delete_button_clicked callback.

Dave Cook  



try:
    import pygtk; pygtk.require('2.0')
except:
    # Hope for the best...
    pass
import gobject
import gtk
from gtk import mainquit
import gtk.glade

GLADEFILE = 'simple_treestore.glade'
COLUMN_TITLES = ('Composer', 'Title')
COLUMN_TYPES = [gobject.TYPE_STRING]*2 + [gobject.TYPE_BOOLEAN]*2

treedata = [[0, 'Mahler, Gustav', 1, '', 2, 1, 3, 0],
            [0, '', 1, 'Symphony No 1', 2, 0, 3, 1],
            [0, '', 1, 'Symphony No 9', 2, 0, 3, 1],
            [0, 'Prokofiev, Sergei', 1, '', 2, 1, 3, 0],
            [0, '', 1, 'Alexander Nevsky', 2, 0, 3, 1],
            [0, '', 1, 'Piano Concerto No 3', 2, 0, 3, 1],
            [0, 'Bartok, Bela', 1, '', 2, 1, 3, 0],
            [0, '', 1, 'Concerto for Orchestra', 2, 0, 3, 1],
            [0, '', 1, 'String Quartet No 5', 2, 0, 3, 1]]

        
def on_delete_button_clicked(treeview):
    selection = treeview.get_selection()
    select_tuple = selection.get_selected()
    if select_tuple:
        store, iter = select_tuple
        store.remove(iter)

def edited_callback(renderer, path_string, newtext, treeview):
    colno = renderer.get_data('colno')
    if colno!=None:
        indices = path_string.split(':')
        path = tuple( map(int, indices) )
        store = treeview.get_model()
        iter = store.get_iter(path)
        store.set_value(iter, colno, newtext)
        
def add_columns(treeview, column_titles):
    ncols = len(column_titles)
    for i in range(ncols):
        cell = gtk.CellRendererText()
        column = gtk.TreeViewColumn(column_titles[i], cell, text=i,
                                    editable=ncols+i, visible=ncols+i)
        cell.set_data('colno', i)
        cell.connect("edited", edited_callback, treeview)
        treeview.append_column(column)

def init_store(store, rows):
    for row in rows:
        if row[-1]==0:
            iter = store.append(None)
            parent = iter
        else:
            iter = store.append(parent)
        store.set(iter, *row)

def main():
    xml = gtk.glade.XML(GLADEFILE)
    treeview = xml.get_widget('treeview')
    add_columns(treeview, COLUMN_TITLES)
    xml.signal_autoconnect(globals())
    store = gtk.TreeStore(*COLUMN_TYPES)
    init_store(store, treedata)
    treeview.set_model(store)
    gtk.main()
    
if __name__=='__main__':
    main()



        
        
<?xml version="1.0" standalone="no"?> <!--*- mode: xml -*-->
<!DOCTYPE glade-interface SYSTEM "http://glade.gnome.org/glade-2.0.dtd";>

<glade-interface>

<widget class="GtkWindow" id="window1">
  <property name="visible">True</property>
  <property name="title" translatable="yes">window1</property>
  <property name="type">GTK_WINDOW_TOPLEVEL</property>
  <property name="window_position">GTK_WIN_POS_NONE</property>
  <property name="modal">False</property>
  <property name="default_width">240</property>
  <property name="default_height">240</property>
  <property name="resizable">True</property>
  <property name="destroy_with_parent">False</property>
  <signal name="delete_event" handler="mainquit" last_modification_time="Sun, 27 Jul 
2003 12:15:57 GMT"/>

  <child>
    <widget class="GtkVBox" id="vbox1">
      <property name="visible">True</property>
      <property name="homogeneous">False</property>
      <property name="spacing">0</property>

      <child>
        <widget class="GtkScrolledWindow" id="scrolledwindow1">
          <property name="border_width">3</property>
          <property name="visible">True</property>
          <property name="can_focus">True</property>
          <property name="hscrollbar_policy">GTK_POLICY_AUTOMATIC</property>
          <property name="vscrollbar_policy">GTK_POLICY_AUTOMATIC</property>
          <property name="shadow_type">GTK_SHADOW_ETCHED_IN</property>
          <property name="window_placement">GTK_CORNER_TOP_LEFT</property>

          <child>
            <widget class="GtkTreeView" id="treeview">
              <property name="visible">True</property>
              <property name="can_focus">True</property>
              <property name="headers_visible">True</property>
              <property name="rules_hint">False</property>
              <property name="reorderable">False</property>
              <property name="enable_search">True</property>
            </widget>
          </child>
        </widget>
        <packing>
          <property name="padding">0</property>
          <property name="expand">True</property>
          <property name="fill">True</property>
        </packing>
      </child>

      <child>
        <widget class="GtkButton" id="delete_button">
          <property name="visible">True</property>
          <property name="can_focus">True</property>
          <property name="label" translatable="yes">Delete Row</property>
          <property name="use_underline">True</property>
          <property name="relief">GTK_RELIEF_NORMAL</property>
          <signal name="clicked" handler="on_delete_button_clicked" object="treeview" 
last_modification_time="Sun, 27 Jul 2003 11:55:13 GMT"/>
        </widget>
        <packing>
          <property name="padding">0</property>
          <property name="expand">False</property>
          <property name="fill">False</property>
        </packing>
      </child>
    </widget>
  </child>
</widget>

</glade-interface>
_______________________________________________
pygtk mailing list   [EMAIL PROTECTED]
http://www.daa.com.au/mailman/listinfo/pygtk
Read the PyGTK FAQ: http://www.async.com.br/faq/pygtk/

Reply via email to