如何清除 ttk.Treeview 小部件中的项目? [英] How to clear items from a ttk.Treeview widget?

查看:46
本文介绍了如何清除 ttk.Treeview 小部件中的项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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 是一个列表,显示类似... ('Sugar', '1 Cup') 等等... def 用于选择的组合框,所以我想要的是树视图清晰而不只是不断添加更多成分.不幸的是,我没有看到 clear() 方法.

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.

如果有一种程序化的方式来识别首先是什么(枚举行数会很好......),这让我发疯.我确实在文档中注意到您可以使用 delete 方法,但它想知道要删除的项目是什么...如果我使用:

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)

我明白了

TclError: Item 0 not found

所以我假设它想要像糖"这样的东西......

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

当然是第 22 条,因为如果您选择组合框并想要清除成分树视图,则并非每个食谱中都有相同的成分项目,那么我们如何知道要删除哪些项目?...

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?...

如果您需要更多详细信息,请告诉我...我对使用 treeview 对象还很陌生,但这让我只想在画布上使用两个列表框.

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.

推荐答案

当您在树上插入一个项目时,insert 方法会返回一个项目 ID.这就是你给 delete 方法的东西.

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

此外,给定一个项目 ID(例如根项目),您可以使用 get_children 方法获取其所有子项的列表.如果您没有给 get_children 提供任何参数,它将返回属于根元素的所有项目的列表.然后,您可以遍历此列表以删除项目.

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.

这一切都记录在 treeview 文档docs.python.org.

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

这篇关于如何清除 ttk.Treeview 小部件中的项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆