This message just bounced because the the address was not in the mailing
list. It may be interesting to some people who are looking for a ctree
example.

James.

--
Email: [EMAIL PROTECTED]
WWW:   http://www.daa.com.au/~james/


---------- Forwarded message ----------
>From [EMAIL PROTECTED]  Wed Oct 13 07:34:25 1999
Received: from gateway.bioreason.com (mail.bioreason.com [207.108.245.3])
        by quoll.daa.com.au (8.9.2/8.9.2) with ESMTP id HAA12379
        for <[EMAIL PROTECTED]>; Wed, 13 Oct 1999 07:34:22 +0800 (WST)
Received: from bioreason.com (vizzini [192.168.1.3])
        by gateway.bioreason.com (8.9.3/8.9.3) with ESMTP id RAA04513
        for <[EMAIL PROTECTED]>; Tue, 12 Oct 1999 17:20:53 -0600
Sender: [EMAIL PROTECTED]
Message-ID: <[EMAIL PROTECTED]>
Date: Tue, 12 Oct 1999 17:20:53 -0600
From: Brian Kelley <[EMAIL PROTECTED]>
Organization: Bioreason, Inc.
X-Mailer: Mozilla 4.5 [en] (X11; U; SunOS 5.5.1 sun4m)
X-Accept-Language: en
MIME-Version: 1.0
To: [EMAIL PROTECTED]
Subject: Dynamic GtkCTree example
Content-Type: multipart/mixed;
 boundary="------------8EC7787819597EBFC99B599C"

This is a multi-part message in MIME format.
--------------8EC7787819597EBFC99B599C
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

A lot of people have been looking for a GtkCTree example.  Here is a
filebrowser that dynamically adds directories as the filesystem is
explored.  It covers some good material such as how do you make a
dynamic tree and if a node sends a signal, how do you reference the node
as a lookup into a dictionary.  If there are any specific questions,
I'll be happy to answer them.


To run, just python the file and hope you have pygtk and gtk installed
correctly :)
It is fairly well commented, enjoy!

--
Brian Kelley          w 505 995-8188
Bioreason, Inc        f 505 995-8186
309 Johnson Av
Santa Fe, NM 87501    [EMAIL PROTECTED]



--------------8EC7787819597EBFC99B599C
Content-Type: text/plain; charset=us-ascii;
 name="CTreeFileBrowser.py"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename="CTreeFileBrowser.py"

#!/usr/bin/env python
"""
This module provides very simple dynamic filebrowser.
Its main purpose is to show how to build dynamic trees that don't
have to store the full tree all at once.
"""

# Don't do 'from gtk import *'.  This module needs only a few
# gtk features, and shouldn't fatten its namespace w. gtk symbols.
import gtk, GTK
import os


class FileBrowser:
    def __init__(self, directory="."):
        """Initialize the file browser from a given directory"""
        self.widget = gtk.GtkHBox()
        self.tree = gtk.GtkCTree( cols=2, titles=["Directory", "File"] )
        self.tree.show()


        # keep all the unexpanded paths stored in a dictionary
        self.unexpanded_paths = {}
        # add the base file to the tree
        self.__add_file(directory)

        # make a window
        self.scrolled_win = gtk.GtkScrolledWindow()
        self.scrolled_win.set_policy(gtk.POLICY_AUTOMATIC,
                                     gtk.POLICY_AUTOMATIC)
        # size the window and pack
        self.scrolled_win.set_usize(200, 200)
        self.scrolled_win.show()
        self.widget.pack_start(self.scrolled_win,
                               expand=gtk.TRUE, fill=gtk.TRUE)
        self.scrolled_win.add(self.tree)

        # connect the tree-expand signal (this signal is sent when nodes
        #  are expanded in a tree)
        self.tree.connect("tree-expand", self.tree_expand)

    def show(self):
        """show the FileBrowser"""
        self.widget.show()
        
    def hide(self):
        """hide the FileBrowser"""
        self.widget.hide()
        
    def delete(self):
        """Delete the file browser"""
        # well, hide it actually
        self.widget.hide()
        
    def tree_expand(self, tree, node):
        """catch the tree-expand signal, if the directory has not
        been updated, read all of the files in the directory and
        add them to the tree.  Remove the directory from the list
        of unexpanded directories and remove the bookeeping node"""
        try:
            # this try block freezes the tree updates until
            # all information to be displayed is actually inside the
            # tree.  If anything fails inside this block, the finally
            # clauses will repaint the tree.  (This prevents users
            #  from staring at an un-updated window)
            self.tree.freeze()
            path = self.tree.node_get_row_data(node)
            if path in self.unexpanded_paths.keys():
                # add the files in the directory
                expand_node, bookkeeping_node = self.unexpanded_paths[path]
                for file in os.listdir(path):
                    self.__add_file(os.path.join(path, file),
                                   expand_node)
                # remove the path from the list of unexpanded paths
                del self.unexpanded_paths[path]
                # remove the bookeeping node
                self.tree.remove_node(bookkeeping_node)
            # resize the columns
            self.tree.columns_autosize()
        finally:
            # no matter what, always thaw the tree
            self.tree.thaw()

    def __add_file(self, path, parent=None):
        """Add a file/directory to the tree as a child of parent.
        If parent is None, then add to the root of the tree"""
        assert os.path.exists(path), "Whups, path doesn't exist"

        file = os.path.split(path)[-1]
        if os.path.isdir(path):
            is_leaf = 0
            show_path = file
            show_file = ""
        else:
            is_leaf = 1
            show_path = os.path.split(path)[0]
            show_file = file
            
        node=self.tree.insert_node(parent, None,
                                   text = [show_path, show_file],
                                   is_leaf = is_leaf,
                                   expanded = gtk.FALSE)
        
        if not is_leaf:
            # add the bookeeping node
            bookkeep_node = self.tree.insert_node(node, None,
                                                  text=["book", "node"],
                                                  is_leaf = gtk.TRUE,
                                                  expanded = gtk.FALSE)
            # cache the path
            self.unexpanded_paths[path] = (node, bookkeep_node)
            # add the path to the node data so we can look it up
            self.tree.node_set_row_data(node, path)

            
def main():
    """Module mainline (for standalone execution)"""
    window = gtk.GtkWindow()
    window.connect("delete_event", gtk.mainquit)
    t = FileBrowser("/")
    window.add(t.widget)
    t.show()
    window.show()
    gtk.mainloop()


if __name__ == "__main__":
    # this code isn`t executed when importing
    main()

--------------8EC7787819597EBFC99B599C--

To unsubscribe: echo "unsubscribe" | mail [EMAIL PROTECTED]

Reply via email to