TTK Treeview不显示子树

时间:2017-01-12 11:00:43

标签: python-3.x tkinter treeview ttk

我正在使用Python3 TTK Treeview来显示树。但首先,子节点没有缩进,只有树的第一个子级可见。父子节点不显示其子节点。

from tkinter import *
from tkinter.ttk import *

Win = Tk()

entries_tree = Treeview(Win, columns = ("Glyph", "Name"), show="tree")
entries_tree.column("#0", width = 20)
entries_tree.column("#1", width = 20)
entries_tree.pack()
#Test
Un = entries_tree.insert("", "end", values = ["X", "Un"])
Deux = entries_tree.insert("", "end", values = ["X", "Deux"])
Trois = entries_tree.insert(Un, "end", values = ["X", "Un Un"], open = True)
Quatre = entries_tree.insert(Un, "end", values = ["X", "Un Deux"])
Cinq = entries_tree.insert(Un, "end", values = ["X", "Un Trois"])
Six = entries_tree.insert(Trois, "end", values = ["X", "Un Un Un"])
Sept = entries_tree.insert(Trois, "end", values = ["X", "Un Un Deux"])

Win.mainloop()

结果: 第一次显示
Fist display

扩展的第一个父节点:作为父节点的第一个子节点(Trois)不显示扩展可能性字形,并且子节点不缩进。

First parent node expanded.

1 个答案:

答案 0 :(得分:4)

欢迎。请花时间将您的问题附加到主要问题部分。这将有助于其他未来用户从您的问题中学习并满足stackoverflow的标准。

要允许您查看目录,即树结构,您需要:

  1. 加宽栏"#0"。默认值20足够宽,可以看到1个指标,而不是其子项。
  2. text = your_label_name 选项添加到 .insert 方法中以识别树节点。
  3. 您还可以添加" open = True"在' Un'的.insert方法中。向孩子们展示Un'在开始时。

    from tkinter import *
    from tkinter.ttk import *
    
    Win = Tk()
    
    entries_tree = Treeview(Win, columns = ("Glyph", "Name"), show="tree")
    entries_tree.column("#0", width = 120, stretch=0)
    entries_tree.column("#1", width = 20, stretch=0)
    entries_tree.pack()
    #Test
    Un = entries_tree.insert("", "end", text='Un', values = ["X", "Un"], open = True)
    Deux = entries_tree.insert("", "end", text='Deux', values = ["X", "Deux"])
    Trois = entries_tree.insert(Un, "end", text='Trois', values = ["X", "Un Un"], open = True)
    Quatre = entries_tree.insert(Un, "end", text='Quatre', values = ["X", "Un Deux"])
    Cinq = entries_tree.insert(Un, "end", text='Cinq', values = ["X", "Un Trois"])
    Six = entries_tree.insert(Trois, "end", text='Six', values = ["X", "Un Un Un"])
    Sept = entries_tree.insert(Trois, "end", text='Sept', values = ["X", "Un Un Deux"])
    
    Win.mainloop()
    

    treeview