Skip to content Skip to sidebar Skip to footer

Python Tkinter: Tree Double-click Node

I have created 2 trees with idlelib.TreeWidget in Canvas, left and right. I am able to print out the name of a tree node if double-clicked, but what I need is double-clicking tree

Solution 1:

You just need to modify the DomTreeItem class to take an argument that determines if it should act on double-click or not:

classDomTreeItem(TreeItem):
   def__init__(self, node, doubleclick=True): # set the value of double-click
      self.node = node
      self.doubleclick = doubleclick           # make the value an instance variabledefGetText(self):
      node = self.node
      if node.nodeType == node.ELEMENT_NODE:
         return node.nodeName
      elif node.nodeType == node.TEXT_NODE:
         return node.nodeValue
   defIsExpandable(self):
      node = self.node
      return node.hasChildNodes()
   defGetSubList(self):
      parent = self.node
      children = parent.childNodes
      prelist = [DomTreeItem(node, self.doubleclick) for node in children] # pass it to the nodes
      itemlist = [item for item in prelist if item.GetText().strip()]
      return itemlist
   defOnDoubleClick(self):
      if self.doubleclick:         # check if it's set to Trueprint self.node.nodeName  # and only print it then

Then, when you make a new instance of the class, just set doubleclick to True or False. If you don't want a double-click to trigger on the second tree, instantiate it like this:

item2 = DomTreeItem(dom2.documentElement, doubleclick=False)

Post a Comment for "Python Tkinter: Tree Double-click Node"