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.
The Treeview widget items can be edited and deleted by selecting the item using tree. selection() function. Once an item is selected, we can perform certain operations to delete or edit the item.
TreeView enables you to search for the nodes matching the specified string. To search for a node in TreeView, you can use Search or SearchAll method of C1TreeView class.
The Treeview widget is used to display a list of items with more than one feature in the form of columns. By default, the listed items in a Treeview widget can be selected multiple times, however you can disable this feature by using selectmode="browse" in the Treeview widget constructor.
By default, the TreeView keeps the focus through hierarchical item indices. The indices are zero-based and the first root item has a 0 (zero) index. If the first root item has children, the first child acquires a 0_0 index, the second child—a 0_1 index, and so on.
To just make the code a bit more concise and Pythonic:
map(ingredients.delete, ingredients.get_children())
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With