简体   繁体   English

Wxpython:TreeCtrl:对树进行迭代

[英]Wxpython: TreeCtrl: Iteration over a tree

I am using the following method to iterate over all the nodes of a wxpython treectrl.我正在使用以下方法迭代 wxpython treectrl 的所有节点。

 def get_desired_parent(self, name, selectednode = None):
    if selectednode == None:
        selectednode = self.treeCtrl.RootItem
    # First perform the action on the first object separately
    childcount = self.treeCtrl.GetChildrenCount(selectednode, False)
    if childcount == 0:
        return None

    (item,cookie) = self.treeCtrl.GetFirstChild(selectednode)
    if self.treeCtrl.GetItemText(item) == name:
        return item

    while childcount > 1:
        childcount = childcount - 1
     # Then iterate over the rest of objects
        (item,cookie) = self.treeCtrl.GetNextChild(item,cookie)
        if self.treeCtrl.GetItemText(item) == name:
            return item
    return None

This problem of excess code becomes even more apparent when I am iterating inside the structure recursively.当我在结构内部递归迭代时,多余代码的问题变得更加明显。 Is there another way of performing the same actions in more compact manner, to make my code more concise / pythonic.是否有另一种以更紧凑的方式执行相同操作的方法,以使我的代码更简洁/pythonic。

You could use a function that is inside this one (in its namespace only) that will check if it matches the conditiin or not.您可以使用此函数内部(仅在其命名空间中)的函数来检查它是否与条件匹配。 If it does return the item if it doesn't, continue.如果它确实返回了该项目,如果它没有返回,请继续。

Otherwise you could check your condition just after the while line.否则,您可以在while行之后检查您的状况。 This way the item variable will be defined by the first child before the loop and evaluated like any other.这样, item变量将由循环之前的第一个子item定义,并像其他任何项一样进行评估。

Still another way: (or a mix of the two)还有一种方式:(或两者的混合)

(child, cookie) = self.GetFirstChild(item)
while child.IsOk():
    do_something(child)
     (child, cookie) = self.GetNextChild(item, cookie)

Here a full example that traverses the tree going depth first.这是一个完整的示例,首先遍历树的深度。 The function was bound to the right button.该功能绑定到右侧按钮。

def OnRightDown(self, event):
    def showChildren(item,cookie):
        # functions goes recursively down the tree
        if item.IsOk():
            child, cookie = self.tree.GetFirstChild(item)
            while child.IsOk():
                child, cookie = self.tree.GetNextChild(child, cookie)
                if child:
                  print(self.tree.GetItemText(child)) #show child label name
                  showChildren(child,cookie)
    pt = event.GetPosition()
    item, flags = self.tree.HitTest(pt)
    if item:
        print(self.tree.GetItemText(item)) #show parent label name
        showChildren(item,0)  #iterate depth first into the tree

The best way to make your code highly readable here is to make it short and highly functional.使您的代码在此处具有高度可读性的最佳方法是使其简短且功能强大。

If you need to iterate through all the tree items and do so through depth first.如果您需要遍历所有树项并首先遍历深度。 Here's that as a single quick function.这是一个单一的快速功能。 Hand it a function that gets each item, and where you start (usually self.root).给它一个函数来获取每个项目,以及你从哪里开始(通常是 self.root)。 It's also quite reusable since you might be doing this a lot.它也非常可重用,因为您可能经常这样做。

    def depth_first_tree(self, funct, item):
        (child, cookie) = self.tree.GetFirstChild(item)
        while child.IsOk():
            self.depth_first_tree(funct, child)
            funct(child)
            (child, cookie) = self.tree.GetNextChild(item, cookie)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM