任何善意帮助的人。我确定我做错了什么,但如果我知道它是什么,我该死的。
作为在ttk.Treeview组件中创建目录列表的初步,我正在尝试自学如何将项添加到树中并列出它们。我编写的简单测试例程应该将新子项添加到树中的最后一项。不幸的是,getchildren()创建的列表总是只有一个项目,只包含根,新项目总是作为根的子项添加 - 并且不会出现在getchildren()的列表中。项目本身已添加:
以下是相关代码:
# Create a treeview object.
tree1 = ttk.Treeview(left_frame)
tree1.insert('', 'end', 'tree-view test', text="Tree View Root")
# And put it in the window.
tree1.pack()
def tree_experiment(tree_current):
for i_counter in range(10):
list_children = tree_current.get_children()
for child_item in list_children:
print(child_item)
last_item = list_children[len(list_children) - 1]
str_counter = str(i_counter)
test_item = tree_current.insert(last_item, 'end', '', text='Inserted item ' + str_counter)
if tree_current.exists(test_item):
print("Found new item in tree: {}".format(test_item))
else:
print("New item in tree not found")
print(str(len(list_children)) + " items in the tree")
输出结果为:
tree-view test
Found new item in tree: I001
1 items in the tree
tree-view test
Found new item in tree: I002
1 items in the tree
tree-view test
Found new item in tree: I003
1 items in the tree
tree-view test
Found new item in tree: I004
1 items in the tree
tree-view test
Found new item in tree: I005
1 items in the tree
tree-view test
Found new item in tree: I006
1 items in the tree
tree-view test
Found new item in tree: I007
1 items in the tree
tree-view test
Found new item in tree: I008
1 items in the tree
tree-view test
Found new item in tree: I009
1 items in the tree
tree-view test
Found new item in tree: I00A
1 items in the tree
这是一个截图,显示已添加的项目,如果仅作为根的子项:
如果有人可以提供帮助,谢谢,如果我重复了别人的问题,我会道歉。
答案 0 :(得分:0)
我已经弄明白了。 getchildren()只返回节点的直接子节点,而不是递归地返回所有子节点。通过跟踪最后添加的节点,我能够获得我想要的效果。