如何使用Tkinter清除整个树视图

41
我的程序使用 ttk.Treeview 作为表格,并用许多数字填充它。
我希望在窗口中按下按钮时清空 ttk.Treeview
有没有简单的方法来清除 ttk.Treeview
谢谢。
4个回答

120

更加简单:

tree.delete(*tree.get_children())

5
为什么那里有一个 * - buhtz
6
这是“展开”运算符:参见这个问题 - James Adam

23

好的,我找到了。如果有人需要我的问题的答案,我会发布答案:

for i in tree.get_children():
    tree.delete(i)

1
我尝试了,但它没有从树中删除行。在创建树时是否缺少任何设置? - Rahul Varadkar

0

将行定义为来自名为Table的TreeView中的单个数据

for row in Table.get_children():
   Table.delete(row)


0
继续Katze的回答,当你删除树之后,你可能需要更新窗口以反映更改。
for i in tree.get_children():
   tree.delete(i)
window.update()

你需要将程序放在一个 while true 循环中,而不是主循环中。

while True:
  if condition_check():
      for i in tree.get_children():
         tree.delete(i)
      window.update()

请您添加一个有用的代码片段吗?虽然很不可能,但是该答案有时可能会被删除,使得这个答案变得不那么有帮助。 :) - TheTechRobo the Nerd
为什么要在tkinter中使用while循环?.mainloop()是运行tkinter应用程序的最佳方式。 - ego-lay atman-bay

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接