https://pythonguides.com/python-tkinter-treeview/#Python_Tkinter_Treeview_Table
1 import tkinter as tk 2 from tkinter import ttk 3 from tkinter import * 4 5 ws = tk.Tk() 6 ws.title('PythonGuides') 7 ws.geometry('1000x800') 8 # ws['bg']='#fb0' 9 frame1 = ttk.Frame(ws, borderwidth=5, relief="groove", width=100, height=100) 10 frame1.grid(row=0, column=0) 11 frame1.propagate(0) 12 ttk.Label(frame1, text="Explaining Python Tkinter with an Example", background="green").grid(row=0, column=0) 13 14 tv = ttk.Treeview(frame1) 15 tv['columns'] = ('Rank', 'Name', 'Badge') 16 tv.column('#0', width=0, stretch=tk.NO) 17 tv.column('Rank', anchor=tk.CENTER, width=80) 18 tv.column('Name', anchor=tk.CENTER, width=80) 19 tv.column('Badge', anchor=tk.CENTER, width=80) 20 21 tv.heading('#0', text='', anchor=tk.CENTER) 22 tv.heading('Rank', text='Id', anchor=tk.CENTER) 23 tv.heading('Name', text='rank', anchor=tk.CENTER) 24 tv.heading('Badge', text='Badge', anchor=tk.CENTER) 25 26 tv.insert(parent='', index=0, iid=0, text='', values=('1', 'Vineet', 'Alpha')) 27 tv.insert(parent='', index=1, iid=1, text='', values=('2', 'Anil', 'Bravo')) 28 tv.insert(parent='', index=2, iid=2, text='', values=('3', 'Vinod', 'Charlie')) 29 tv.insert(parent='', index=3, iid=3, text='', values=('4', 'Vimal', 'Delta')) 30 tv.insert(parent='', index=4, iid=4, text='', values=('5', 'Manjeet', 'Echo')) 31 tv.grid(row=1, column=0, sticky="W") 32 33 34 def update_item(): 35 selected = tv1.focus() 36 print(f'selected={selected}') # selected=3 37 temp = tv1.item(selected, 'values') 38 print(f'temp={temp}') # temp=('Shanti', 'e14', '22000.0') 39 sal_up = float(temp[3]) + float(temp[3]) * 0.05 40 tv1.item(selected, values=(temp[0], temp[1], temp[2], sal_up)) 41 42 43 def show_selected(): 44 print(tv1.selection()) 45 46 47 def selectmode_none(): 48 tv1['selectmode'] = "none" 49 print("selectmode=none 用户将不会在 Treeview 上看到任何标记,点击数据无显示标记") 50 51 52 def selectmode_browse(): 53 tv1['selectmode'] = "browse" 54 print("selectmode=browse 用户将能够在一次选择单个项目") 55 56 57 58 def selectmode_extended(): 59 tv1['selectmode'] = "extended" 60 print("selectmode=extended 用户就可以同时选择多个项目(按 shift 键可选择多个项目)") 61 62 # 表格数据 63 datas = [(0, "vineet", "e11", 1000000.00) 64 , (1, "anil", "e12", 120000.00) 65 , (2, "ankit", "e13", 41000.00) 66 , (3, "Shanti", "e14", 22000.00) 67 , (4, "Shanti", "e14", 22000.00) 68 , (5, "Shanti", "e14", 22000.00) 69 , (6, "Shanti", "e14", 22000.00) 70 , (7, "Shanti", "e14", 22000.00) 71 , (8, "Shanti", "e14", 22000.00) 72 , (9, "Shanti", "e14", 22000.00) 73 , (10, "Shanti", "e14", 22000.00) 74 , (11, "Shanti", "e14", 22000.00) 75 ] 76 77 frame2 = ttk.Frame(ws, borderwidth=5, relief="groove", width=300, height=100) 78 frame2.grid(row=0, column=1) 79 frame2.propagate(0) 80 81 sl1 = Scrollbar(frame2) 82 83 conten = "How to change Value in Python Tkinter Treeview ,\n Python Tkinter Treeview Scrollbars" 84 ttk.Label(frame2, text=conten, background="red").grid(row=0, column=0,columnspan=4) 85 columns = ["id", "name", "eid", "Slary"] 86 tv1 = ttk.Treeview(frame2 87 , show='headings' 88 , height=5 89 , columns=columns 90 ) 91 tv1.grid(row=1, column=0,columnspan=4) 92 headText = ("id", "name", "eid", "Slary") 93 for idx in range(len(columns)): 94 tv1.column(columns[idx], width=70, minwidth=70, anchor=tk.CENTER) 95 tv1.heading(idx, text=headText[idx]) 96 97 for i in range(len(datas)): 98 tv1.insert(parent='', index=i, iid=i, values=datas[i]) 99 ttk.Button(frame2, text='Increment Salary', command=update_item).grid(row=2, column=1,columnspan=1) 100 # 创建滚动条 101 scroll = tk.Scrollbar(frame2, orient="vertical", command=tv1.yview) 102 scroll['command'] = tv1.yview 103 tv1.config(yscrollcommand=scroll.set) # 将滚动条填充 104 scroll.grid(row=1, column=5, sticky=S + W + E + N) 105 ttk.Button(frame2, text='Show Selected ', command=show_selected).grid(row=3, column=1,columnspan=1) 106 107 ttk.Button(frame2, text='Browse', command=selectmode_browse).grid(row=4, column=0) 108 ttk.Button(frame2, text='None', command=selectmode_none).grid(row=4, column=1) 109 ttk.Button(frame2, text='Extended', command=selectmode_extended).grid(row=4, column=2) 110 111 112 ws.mainloop()