1. 表格数据显示:
图示:
实例:
import tkinter from tkinter import ttk # 创建主窗口__编程头部 win = tkinter.Tk() # 设置标题 win.title("Thomas的窗口") # 设置大小和位置 win.geometry("600x400+1500+100") #格式:400x400表示大小,200和0表示位置 # 表格数据 tree = ttk.Treeview(win) tree.pack() # 定义列(_注意如下的信息顺序一定要对齐_) tree["columns"] = ("姓名","年龄","身高","体重") # 设置列 tree.column("姓名",width=100) tree.column("年龄",width=100) tree.column("身高",width=100) tree.column("体重",width=100) # 设置表头 tree.heading("姓名",text="姓名-name") tree.heading("年龄",text="年龄-age") tree.heading("身高",text="身高-height") tree.heading("体重",text="体重-weight") # 添加数据 # 0表示数据行索引 tree.insert("",0,text="line1",values=("Thomas","28","165","80")) tree.insert("",1,text="line1",values=("Jerry","18","175","84")) tree.insert("",2,text="line1",values=("Petter","22","185","180")) tree.insert("",4,text="line1",values=("Henry","23","160","85")) # 保持循环不让窗口关闭__编程尾部 win.mainloop()
2. 树状结构显示:
图示:
实例:
import tkinter from tkinter import ttk # 创建主窗口__编程头部 win = tkinter.Tk() # 设置标题 win.title("Thomas的窗口") # 设置大小和位置 win.geometry("600x400+1500+100") #格式:400x400表示大小,200和0表示位置 # 表格数据 tree = ttk.Treeview(win) tree.pack() # 添加一级树枝 treeF1 = tree.insert("",0,"中国",text="中国CHN",values=("F1")) treeF2 = tree.insert("",1,"美国",text="美国USA",values=("F2")) treeF3 = tree.insert("",2,"英国",text="英国UK",values=("F3")) # 添加二级树枝 treeF1_1 = tree.insert(treeF1,0,"黑龙江",text="黑龙江",values=("F1_1")) treeF1_2 = tree.insert(treeF1,1,"辽宁",text="辽宁",values=("F1_2")) treeF1_3 = tree.insert(treeF1,2,"吉林",text="吉林",values=("F1_3")) treeF2_1 = tree.insert(treeF2,0,"得克萨斯州",text="得克萨斯州",values=("F2_1")) treeF2_2 = tree.insert(treeF2,1,"加利福尼亚州",text="加利福尼亚州",values=("F2_2")) treeF2_3 = tree.insert(treeF2,2,"俄亥俄州",text="俄亥俄州",values=("F2_3")) # 添加三级树枝 treeF1_1_1 = tree.insert(treeF1_1,0,"哈尔滨",text="哈尔滨") treeF1_1_2 = tree.insert(treeF1_1,1,"五常",text="五常") # 保持循环不让窗口关闭__编程尾部 win.mainloop()