wxpython 根据item的名字找到tree_ctrl对应的节点

业务场景,需要在一个tree_ctrl中找到一个节点,并且给该节点添加子节点

def get_item_by_label(self, tree, search_text, root_item):
    item, cookie = tree.GetFirstChild(root_item)

    while item.IsOk():
        text = tree.GetItemText(item)
        if text.lower() == search_text.lower():
            return item
        if tree.ItemHasChildren(item):
            match = self.get_item_by_label(tree, search_text, item)
            if match.IsOk():
                return match
        item, cookie = tree.GetNextChild(root_item, cookie)

    return wx.TreeItemId()

result = get_item_by_label(tree, '已有节点', tree.GetRootItem())
if result.IsOk():
    print('We have a match!')
    new_item = tree.AppendItem(result , "新节点")

你可能感兴趣的:(wxpython 根据item的名字找到tree_ctrl对应的节点)