Treeview 基本示例
此小组件用于显示具有层次结构的项目。例如,可以以这种方式再现 Windows 资源管理器。一些不错的表也可以使用 treeview
小部件完成。
创建小部件
tree=ttk.Treeview(master)
列的定义
你可以在用户尝试拉伸时定义多少列,宽度和最小宽度。通过定义 stretch=tk.NO
,用户无法修改列的宽度。
tree["columns"]=("one","two","three")
tree.column("#0", width=270, minwidth=270, stretch=tk.NO)
tree.column("one", width=150, minwidth=150, stretch=tk.NO)
tree.column("two", width=400, minwidth=200)
tree.column("three", width=80, minwidth=50, stretch=tk.NO)
标题的定义
tree.heading("#0",text="Name",anchor=tk.W)
tree.heading("one", text="Date modified",anchor=tk.W)
tree.heading("two", text="Type",anchor=tk.W)
tree.heading("three", text="Size",anchor=tk.W)
插入一些行
# Level 1
folder1=tree.insert("", 1, "", text="Folder 1", values=("23-Jun-17 11:05","File folder",""))
tree.insert("", 2, "", text="text_file.txt", values=("23-Jun-17 11:25","TXT file","1 KB"))
# Level 2
tree.insert(folder1, "end", "", text="photo1.png", values=("23-Jun-17 11:28","PNG file","2.6 KB"))
tree.insert(folder1, "end", "", text="photo2.png", values=("23-Jun-17 11:29","PNG file","3.2 KB"))
tree.insert(folder1, "end", "", text="photo3.png", values=("23-Jun-17 11:30","PNG file","3.1 KB"))
填料
tree.pack(side=tk.TOP,fill=tk.X)
在 Windows 上,可以从此示例获取以下屏幕截图。