Search code examples
pythonpython-3.xtkintertreeview

How to delete a certain row of a TreeView table - Tkinter


Hello I want to know how can I delete a certain row of a TreeView table. For example I want to delete first row of table.

I know that we can delete a row by selecting it like:

selected_item = tree.selection()[0]
tree.delete(selected_item)

But I don't want to select. I just want to delete a certain row with a certain value before actually the whole TreeView display (yeah you would say.. "just don't insert it in the first place" but I want to know).

Please help.


Solution

  • You could first extract all the rows, then iterate through them and delete any rows matching a certain condition. Something like the following (replace the if condition with whatever your deletion criteria is)

    for row in tree.get_children():
        if tree.item(row) == {'col1': 1, 'col2': 2}:
            tree.delete(row)