Python Tkinter: obtain tree node information
Question: Python 2.7 Linux

I am only using idlelib.TreeWidget to create a tree in Tkinter.Canvas, nothing
else.

How do I go about getting the information of the selected tree node (eg.
name)? I need to access this information later.

I am able to call a function when the Canvas is selected / double-clicked, but
not sure how with the tree nodes:

    
    
    self.canvas.bind('<Double-Button-1>', self.onSave)
    

Please run the following code (Note that there are 2 trees, 1 tree in each
Canvas):

    
    
    from Tkinter import Tk, Frame, BOTH, Canvas
    from xml.dom.minidom import parseString
    from idlelib.TreeWidget import TreeItem, TreeNode
    
    class DomTreeItem(TreeItem):
       def __init__(self, node):
          self.node = node
       def GetText(self):
          node = self.node
          if node.nodeType == node.ELEMENT_NODE:
             return node.nodeName
          elif node.nodeType == node.TEXT_NODE:
             return node.nodeValue
       def IsExpandable(self):
          node = self.node
          return node.hasChildNodes()
       def GetSubList(self):
          parent = self.node
          children = parent.childNodes
          prelist = [DomTreeItem(node) for node in children]
          itemlist = [item for item in prelist if item.GetText().strip()]
          return itemlist
    
    data = '''
    <angel>
     <digital_core>
      <radio_top>d</radio_top>
      <uart_top>e</uart_top>
     </digital_core>
     <digital_core>
      <c>f</c>
     </digital_core>
    </angel>
    '''
    class Application(Frame):
    
       def __init__(self, parent):
          Frame.__init__(self, parent, background = "white")
          self.parent = parent
    
          # Maximize window
          self.screenWidth = self.parent.winfo_screenwidth() - 5
          self.screenHeight = self.parent.winfo_screenheight() - 110
          self.parent.geometry('%dx%d+%d+%d' % (self.screenWidth, self.screenHeight, 0, 0))
          self.parent.resizable(0, 0)
    
          dom = parseString(data)
          item = DomTreeItem(dom.documentElement)
          self.canvas = Canvas(self, bg = "cyan")
          self.canvas.grid(column = 0, row = 0, rowspan = 2, sticky = 'NSWE', padx = 5, pady = 5)
          node = TreeNode(self.canvas, None, item)
          node.update()
    
          self.canvas2 = Canvas(self, bg = "yellow")
          self.canvas2.grid(column = 1, row = 0, rowspan = 2, sticky = 'NSWE', padx = 5, pady = 5)
          node2 = TreeNode(self.canvas2, None, item)
          node2.update()
    
          parent.configure(bg = "black")
          self.pack(fill = BOTH, expand = True, padx = 20, pady = 20)
    
    def main():
       root = Tk()
       Application(root)
       root.mainloop()
    
    if __name__ == '__main__':
       main()  
    


Answer: The most direct approach I have seen is accessing the fields of the `TreeNode`
to retrieve content passed into the `TreeNode` constructor.

For instance,

    
    
    node.parent
    node.canvas
    node.item
    

To get the text from the `DomTreeItem`, you can do

    
    
    node.item.GetText()
    

You can also get additional information by looking at the methods in

    
    
    dir(node.item)
    

