[Tkinter-discuss] Difficulty getting started with a BWidget Tree

Michael Lange klappnase at web.de
Thu Sep 15 12:44:09 CEST 2005


On Wed, 14 Sep 2005 23:46:57 -0400
"gilcneth at earthlink.net" <gilcneth at earthlink.net> wrote:

> Hello All:
> 
> I am trying to create a Tree, using PyBWidgets, but am unsure about how to actually create the initial parent and/or child nodes.  The documentation leads me to believe that I should use the' insert' command, but how?  The documentation indicates the following:
> 
> pathName insert index parent node ?option value...?
> 
> So, given my example below, if I want to add a node to my Tree, wouldn't I use the command TopTree.insert?  And, if so, I assume I start at index '0'?  OK, but now who's the parent?  I know this has to be simple, but I am just not figuring this one out.  Please share your thoughts.
> 
Hi Chris,

I wrote a directory Tree widget a while ago, rather a test than a usable widget, but maybe good
enough to get started. I had some problems with my release of pybwidget, because some icons were missing,
I believe the "plus" and "minus" icons for the Tree, but I don't remember exactly.
So, here's the code:

############ file DirTree.py ##############################
import Tkinter as tk
import bwidget as bw
import os

class DirTree(tk.Frame):
    def __init__(self, master, *args, **kw):
        tk.Frame.__init__(self, master)
        self.tree = tree = bw.Tree(self, opencmd=self.open_folder, closecmd=self.close_folder, *args, **kw)
        tree.pack(fill='both', expand=1)
        
        self.folder_icon = tk.PhotoImage(file='/usr/share/pybwidget/images/folder.gif')
        self.openfolder_icon = tk.PhotoImage(file='/usr/share/pybwidget/images/openfold.gif')
        self.file_icon = tk.PhotoImage(file='/usr/share/pybwidget/images/file.gif')
        
        node = self.tree.insert('end', 'root', text='/', image=self.folder_icon, drawcross='allways', data='/')
        self.tree.opentree(node, recurse=0)
    
    def close_folder(self, node):
        self.tree.itemconfigure(node, image=self.folder_icon)
    
    def open_folder(self, node):
        path = self.tree.itemcget(node, 'data')
        children = os.listdir(path)
        files, dirs = [], []
        for item in children:
            if os.path.isdir(os.path.join(path, item)):
                dirs.append(item)
            #else:
            #    files.append(item)
        dirs.sort()
        #files.sort()
        for item in dirs:
            newpath = os.path.join(path, item)
            self.tree.insert('end', node, text=item, image=self.folder_icon, drawcross='allways', data=newpath)
        #for item in files:
        #    newpath = os.path.join(path, item)
         #   self.tree.insert('end', node, text=item, image=self.file_icon, drawcross='never', data=newpath)
        self.tree.itemconfigure(node, image=self.openfolder_icon)
        

def test():
    r = tk.Tk()
    t = DirTree(r, bg='white', selectbackground='blue4', selectforeground='white', deltax=18, deltay=18)
    t.pack(fill='both', expand=1)
    r.mainloop()

if __name__== '__main__':
    test()
##############################################################

I hope this helps

Michael


More information about the Tkinter-discuss mailing list