Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the item count in tkinter treeview?

I inserted several items to the treeview. Can I lookup the overall number of items which are stored in the treeview ? Or even better: Can I iterate over all items like for child in tree:... ?

Python 3.X Tkinter 8.6

like image 737
mr.wolle Avatar asked Dec 20 '22 00:12

mr.wolle


1 Answers

The treeview has a get_children method which can get all children for an item. It's a simple matter of calling that method on the root, and then calling it on every item that has children.

This method is documented in the page that you linked to in your question.

Here's a quick example:

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

On my box it took about 15ms to find all items in a tree that had 11,000 items

like image 57
Bryan Oakley Avatar answered Dec 27 '22 12:12

Bryan Oakley