How to delete all nodes on a tree-view in kivy framework

Viewed 297

I need help in deleting all nodes in a treeview.

I am working on the kivy framework in python that will list some labels.

But what happens while removing node is some of the node are deleted and other nodes still present in the treeview.

Sample code:

for node in treeview.iterate_open_nodes():
   treeview.remove_node(node)
3 Answers

There are many problems in removing items from an iterable while iterating.
You should first collect the items and then remove them

    for node in [i for i in treeview.iterate_all_nodes()]:
       treeview.remove_node(node)

You should use iterate_all_nodes instead of iterate_open_nodes as it iterates only over all the expended nodes

I am using convert to list:

for i in list(treeview.iterate_all_nodes()):
    treeview.remove_node(i)
Related