I'm looking for objective oriented and pythonic way to solve my problem.

I'm using IDLE's TreeWidget, by inheriting TreeItem and overriding some of 
it's methods. My tree structure is defined inside treedata, which is 
xml.dom.minidom object. MenuTreeFrame is responsible for displaying the 
widget inside it's own Frame. As you can see in the source, MyTreeItem is 
one child in tree, and it's method GetSubList is responsible for creating 
it's children TreeItems.

When creating each child, I have to pass original tree structure (treedata) 
to each child. Once I implement event firing, I will have to pass Event 
reference to each child as well. There has to be a better way to make these 
references available to each child in tree. What would you recommend?

My code looks something like this (not executable, because treedata 
definition is lacking):

import Tkinter as tk
from TreeWidget import TreeItem, TreeNode, ScrolledCanvas

class MenuTreeFrame(tk.Frame):
    def __init__(self, parent, treedata):
        tk.Frame.__init__(self, parent)
        self.treedata = treedata
        sc = ScrolledCanvas(self, bg='white', width=150, 
highlightthickness=0, takefocus=1)
        self.canvas = sc.canvas
        sc.frame.pack(fill="both", expand=1)
    def update(self, dom):
        self.create(dom) # TODO: implement something smarter than recreating 
whole tree
    def create(self, dom):
        self.rootitem = DomTreeItem(dom.documentElement, self.treedata)
        self.node = TreeNode(self.canvas, None, self.rootitem)
        self.node.update()
        self.node.select()
        self.rootitem.OnSelect()
        self.node.expand()

class MyTreeItem(TreeItem):
    def __init__(self, treenode, treedata):
        self.node = treenode
        self.previously_selected = None
        self.treedata = treedata
    def GetText(self):
        return self.node.firstChild.nodeValue
    def IsExpandable(self):
        node = self.node
        return len(self.node.getElementsByTagName('node')) > 0
    def OnSelect(self):
        self.treedata.current = self.node
        # TODO: fire off the event
    def GetSubList(self):
        parent = self.node
        children = parent.childNodes
        itemlist = [MyTreeItem(node, self.treedata) for node in children if 
node.nodeName == 'node']
        return itemlist


Thanks for reading
Josip 


-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to