How to clear items from a ttk.Treeview widget?

12,773

Solution 1

When you insert an item on the tree, the insert method returns an item id. This is what you give to the delete method.

Also, given an item id (such as the root item), you can get a list of all of its children with the get_children method. If you do not give any arguments to the get_children it will return a list of all the items that belong to the root element. You can then iterate over this list to delete the items.

This is all documented in the treeview docs at docs.python.org.

Solution 2

To just make the code a bit more concise and Pythonic:

map(ingredients.delete, ingredients.get_children())
Share:
12,773

Related videos on Youtube

Randall.Cummins
Author by

Randall.Cummins

Updated on June 04, 2022

Comments

  • Randall.Cummins
    Randall.Cummins almost 2 years
    ing_scroll = Scrollbar(window1_frame1, orient=VERTICAL)
    ingredients = ttk.Treeview(window1_frame1, yscrollcommand=ing_scroll.set, height=5, columns=['Ingredient', 'Amount'], show="headings")
    ingredients.heading("Ingredient", text='Ingredient')
    ingredients.column("Ingredient", width=7)
    ingredients.heading("Amount", text='Amount')
    ingredients.column("Amount", width=1)
    ing_scroll.config(command=ingredients.yview)
    ing_scroll.pack(side=RIGHT, fill=Y)
    ingredients.pack(side=LEFT, fill='both', expand=1)
    
    def OnRecpSelect(event):
        DB = menu_combo.get()
        mytable = recipe_combo.get()
        ingredient_list = TKengine.pull_ingredients(DB, mytable)
        # NEED TO CLEAR THE INGREDIENTS TTK:TREEVIEW OBJECT HERE!
        for i in ingredient_list: 
            ingre = i[1]
            amoun = i[2]
            value = ingre,amoun
            ingredients.insert('',0,values=value)
    

    ingredient_list is a list that displays something like... ('Sugar', '1 Cup') and so on... The def is for a combobox that is selected, so what I would like is for the treeview to clear and not just keep adding more ingredients. Unfortunately I don't see a clear() method.

    If theres a programmatic way of identifying what is there first (enumerating a rowcount would be good...) this is driving me nuts. I did notice in the docs that you can use the delete method, but it wants to know what the item is to delete... if I use:

    ingredients.delete('',0)
    

    I get

    TclError: Item 0 not found
    

    So I would assume it wants something like 'Sugar' as the Item...

    of course its a catch 22 because if you select the combobox and want to clear the ingredients treeview, the same ingredient items are not in every recipe, so how do we know what items to delete?...

    Please let me know if you need any more details... I am fairly new to working with the treeview object, but its making me want to just work with two listboxes on a canvas.

  • Randall.Cummins
    Randall.Cummins over 12 years
    Agh, Thanks Bryan! I think I saw that get_children method and didn't know what to do with the I001, I002 items, but it makes sense to use something like I001, I002 to use something like tree.delete('',I001) i guess id have to use a try/except if theres no items present when the combobox is selected the first time and theres no items present in the treeview. Anyhow, thanks so much, regenerated my hope.
  • Randall.Cummins
    Randall.Cummins over 12 years
    x = ingredients.get_children() for item in x: ingredients.delete(item) Thank you so much Bryan!
  • anonymoose
    anonymoose over 6 years
    Make sure to wrap one of list(), set(), tuple(), etc. around it in Python 3, though.
  • sharat87
    sharat87 about 6 years
    Also, ingredients.delete(*ingredients.get_children()).