如何在 tkinter 树视图中获取项目计数?

How to get the item count in tkinter treeview?

我在 treeview 中插入了几项。 我可以查找存储在树视图中的项目总数吗? 甚至更好:我可以遍历 所有 项,例如 for child in tree:... ?

Python 3.X Tkinter 8.6

树视图有一个 get_children 方法可以获取一个项目的所有子项。在根上调用该方法,然后在每个有子项的项目上调用它是一件简单的事情。

您在问题中链接到的页面中记录了此方法。

这是一个简单的例子:

def get_all_children(tree, item=""):
    children = tree.get_children(item)
    for child in children:
        children += get_all_children(tree, child)
    return children

在我的盒子上,找到一棵有 11,000 个项目的树中的所有项目需要大约 15 毫秒

len(tree.get_children()) 在 8.6

中适用于我的 ttk.Treeview