tkinter表格数据-Treeview
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
import tkinter
from tkinter import ttk #导入内部包
win = tkinter.Tk()
# 实例化表格
# 空白首列是显示的,设置show属性为 headings 即可隐藏首列。
tree = ttk.Treeview(
win,
height=18,
show="headings",
)
# 列索引ID
tree["columns"] = ("id", "name", "age", "heigh")
# 表头设置
tree.heading("id", text="ID")
tree.heading("name", text="姓名")
tree.heading("age", text="年龄")
tree.heading("heigh", text="身高")
# 表列设置, 可设置对应列的样式等, 不显示
# tree.column("id", width=100)
# tree.column("name", width=100, anchor="center")
# tree.column("age", width=100, anchor="center")
# tree.column("heigh", width=100, anchor="center")
# 插入数据
tree.insert("", 0, text="首列显示内容", values=("135", "张三", "3", "170"))
tree.insert("", 1, text="", values=("136", "李四", "34", "171"))
tree.insert("", 2, text="", values=("137", "王五", "9", "169"))
tree.insert("", 3, text="", values=("138", "马六", "23", "175"))
tree.insert("", 3, text="", values=("138", "马六", "23", "175"))
tree.insert("", 3, text="", values=("138", "马六", "23", "175"))
tree.insert("", 3, text="", values=("138", "马六", "23", "175"))
tree.insert("", 3, text="", values=("138", "马六", "23", "175"))
tree.insert("", 3, text="", values=("138", "马六", "23", "175"))
tree.insert("", 3, text="", values=("138", "马六", "23", "175"))
tree.insert("", 3, text="", values=("138", "马六", "23", "175"))
tree.insert("", 3, text="", values=("138", "马六", "23", "175"))
tree.insert("", 3, text="", values=("138", "马六", "23", "175"))
tree.insert("", 3, text="", values=("138", "马六", "23", "175"))
tree.insert("", 3, text="", values=("138", "马六", "23", "175"))
tree.insert("", 3, text="", values=("138", "马六", "23", "175"))
tree.insert("", 3, text="", values=("138", "马六", "23", "175"))
tree.insert("", 3, text="", values=("138", "马六", "23", "175"))
tree.insert("", 3, text="", values=("138", "马六", "23", "175"))
tree.insert("", 3, text="", values=("138", "马六", "23", "175"))
tree.insert("", 3, text="", values=("138", "马六", "23", "175"))
vbar = ttk.Scrollbar(win, orient="vertical", command=tree.yview)
tree.configure(yscrollcommand=vbar.set)
# tree.pack()
# vbar.pack()
tree.grid(row=3, column=0, columnspan=1, sticky="NSEW")
vbar.grid(row=3, column=1, sticky="NS")
# tree.pack()
win.mainloop()
如果觉得《python表格控件_tkinter 控件 Treeview 表格数据》对你有帮助,请点赞、收藏,并留下你的观点哦!