任何好心帮助我的人。我确信我做错了什么,但如果我知道那是什么,我就完了。
作为在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下面的屏幕截图显示了已经添加的项,如果只是作为根的子项的话:

如果有人能帮上忙,谢谢,如果我重复了别人的问题,我很抱歉。
发布于 2018-02-21 02:01:41
我已经弄清楚了。getchildren()只返回节点的直接子节点,而不是所有的子节点。通过跟踪添加的最后一个节点,我能够获得我想要的效果。
https://stackoverflow.com/questions/48889288
复制相似问题