如何清除 ttk.Treeview 小部件中的项目?

发布于 2024-12-26 07:49:20 字数 1383 浏览 2 评论 0原文

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)

配料列表是一个列表,显示类似...(“糖”,“1杯”)等内容... def是针对选定的组合框,所以我想要的是清除树视图而不是只要继续添加更多成分即可。不幸的是,我没有看到 clear() 方法。

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

ingredients.delete('',0)

我得到

TclError: Item 0 not found

所以我会假设它想要像“糖”这样的项目作为项目.. 当然,

这是第 22 个问题,因为如果您选择组合框并想要清除成分树视图,则并非每个配方中都有相同的成分项目,所以我们如何知道要删除哪些项目?...

请告诉我,如果您需要更多详细信息...我对使用树视图相当陌生对象,但它让我只想在画布上使用两个列表框。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一瞬间的火花 2025-01-02 07:49:20

为了让代码更加简洁和Pythonic:

map(ingredients.delete, ingredients.get_children())

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

map(ingredients.delete, ingredients.get_children())
梦一生花开无言 2025-01-02 07:49:20

当您在树上插入一个项目时,insert 方法会返回一个项目 ID。这就是您赋予删除方法的内容。

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

这一切都记录在 treeview 文档 中,位于 docs.python.org

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文