Hi,

many thanks for the explanation!  I still have a (hopefully
small) problem in getting things running.  I attached an
example, where the tree view itself seems to show up, but
not it's labels or any contents.  I'm using pygtk 1.99.12
on Debian unstable.

On Mon, Jun 24, 2002 at 12:05:24PM +0800, James Henstridge wrote:
> At the moment, Libglade will only be able to place the GtkTreeView 
> widgets.  You will need to connect a model to the view, and set up the 
> columns manually (I am looking at getting libglade to handle things like 
> canvas widgets, tree view columns, etc to work in 2.2, but haven't 
> implemented any of that yet).  You can get a reference to the tree view 
> from the GladeXML object just as you would with any other widget. 
> (xml.get_widget('widget_name')).

That's what I now tried, but it seems that I still do
something very stupid - or better: I forget to do something
clever, but what?

> For an example of filling a GtkTreeStore object (a class that implements 
> the GtkTreeModel interface) with some data, see 
> pygtk/examples/pygtk-demo/pygtk-demo.py.  To set the model for a tree 
> view, use the set_model() method.  In pygtk-demo.py, there is also an 
> example of setting up columns.

That example is very useful.  I copied most of the code from
list_story.py, but simplified it.

> I think I posted an overview of how the tree model and tree view relate 
> in another post to this list a few months ago.  You can probably find it 
> in the archives.

Searched the archives, but unsuccesful :-(

Maybe someone on the list sees immediately, what is missing?
The example is simple enough to be included in an FAQ or
example directory, when it finally works.

Thanks in advance!

Cheers!
#!/usr/bin/env python2.2

import gobject
import gtk
import gtk.glade

class MyTree:
    COLUMN_BOOLEAN = 0
    COLUMN_STRING  = 1
    def __init__(self, treeview):
        self.model = gtk.TreeStore(gobject.TYPE_BOOLEAN,
                                   gobject.TYPE_STRING)
        self.treeview = treeview
        self.add_columns()

    def add_data(self, data):
        iter = None
        for item in data:
            iter = self.model.append(iter)
            self.model.set(iter,
                           self.COLUMN_BOOLEAN, item[0],
                           self.COLUMN_STRING, item[1])

    def toggle(self, cell, path):
        iter = self.model.get_iter_from_string(path)
        self.model.set(iter, self.COLUMN_BOOLEAN,
                       not self.model.get_value(iter, self.COLUMN_BOOLEAN))

    def change(self, rend, path, text):
        iter = self.model.get_iter_from_string(path)
        self.model.set_value(iter, self.COLUMN_STRING, text)

    def add_columns(self):
        renderer = gtk.CellRendererToggle()
        renderer.connect('toggled', self.toggle)
        column = gtk.TreeViewColumn(
            'boolean', renderer, active=self.COLUMN_BOOLEAN)
        column.set_clickable(gtk.TRUE)
        column.set_sizing(gtk.TREE_VIEW_COLUMN_AUTOSIZE)
        self.treeview.append_column(column)

        renderer = gtk.CellRendererText()
        renderer.connect('edited', self.change)
        column = gtk.TreeViewColumn(
            'string', renderer, text=self.COLUMN_STRING, editable=gtk.TRUE)
        column.set_clickable(gtk.TRUE)
        column.set_sizing(gtk.TREE_VIEW_COLUMN_AUTOSIZE)
        self.treeview.append_column(column)

def on_window1_delete_event(obj, event):
    gtk.main_quit()
    return gtk.TRUE

data = [[gtk.FALSE, 'some text'], # v    [ ] some text
        [gtk.TRUE,  'some text'], #  v   [x] some text
        [gtk.FALSE, 'some text'], #   v  [ ] some text
        [gtk.TRUE,  'some text']] #    > [x] some text

def main():
    xml = gtk.glade.XML("treetest.glade")
    win = xml.get_widget("window1")
    xml.signal_autoconnect({
        "on_window1_delete_event":  on_window1_delete_event})
    tree = MyTree(xml.get_widget("treeview1"))
    tree.add_data(data)
    gtk.mainloop()

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="resizable">True</property>
  <property name="destroy_with_parent">True</property>
  <signal name="delete_event" handler="on_window1_delete_event" 
last_modification_time="Wed, 24 Jul 2002 14:08:23 GMT"/>

  <child>
    <widget class="GtkScrolledWindow" id="scrolledwindow1">
      <property name="visible">True</property>
      <property name="can_focus">True</property>
      <property name="hscrollbar_policy">GTK_POLICY_ALWAYS</property>
      <property name="vscrollbar_policy">GTK_POLICY_ALWAYS</property>
      <property name="shadow_type">GTK_SHADOW_NONE</property>
      <property name="window_placement">GTK_CORNER_TOP_LEFT</property>

      <child>
        <widget class="GtkTreeView" id="treeview1">
          <property name="visible">True</property>
          <property name="can_focus">True</property>
          <property name="headers_visible">True</property>
          <property name="rules_hint">True</property>
          <property name="reorderable">True</property>
          <property name="enable_search">True</property>
        </widget>
      </child>
    </widget>
  </child>
</widget>

</glade-interface>

Reply via email to