首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在树上使用.getchildren()总是返回长度为1的列表

在树上使用.getchildren()总是返回长度为1的列表
EN

Stack Overflow用户
提问于 2018-02-20 23:57:54
回答 1查看 446关注 0票数 0

任何好心帮助我的人。我确信我做错了什么,但如果我知道那是什么,我就完了。

作为在ttk.Treeview组件中创建目录列表的初步准备,我正在尝试自学如何向树中添加项目并列出它们。我编写的简单测试例程应该向树中的最后一项添加一个新的子项。不幸的是,getchildren()创建的列表总是只有一项长,只包含根,并且新的项总是作为根的子项被添加-并且不会出现在getchildren()的列表中。将添加项目本身:

相关代码如下:

代码语言:javascript
复制
# 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")

输出为:

代码语言:javascript
复制
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

下面的屏幕截图显示了已经添加的项,如果只是作为根的子项的话:

如果有人能帮上忙,谢谢,如果我重复了别人的问题,我很抱歉。

EN

回答 1

Stack Overflow用户

发布于 2018-02-21 02:01:41

我已经弄清楚了。getchildren()只返回节点的直接子节点,而不是所有的子节点。通过跟踪添加的最后一个节点,我能够获得我想要的效果。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48889288

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档