1、Treeview的基本屬性
- 常用參數意義
①master=win, # 父容器
②height=10, # 表格顯示的行數,height行
③columns=columns, # 顯示的列
④show='headings', # 隱藏首列
⑤heading() # 定義表頭
⑥column()#定義列
⑦anchor='w'#對齊方式,可選n, ne, e, se, s, sw, w, nw, center
⑧command=lambda: print('學號')#點擊表頭執行的回調函數
⑨minwidth=100#表格的最小列寬
# -*- encoding=utf-8 -*-
import tkinter
from tkinter import *
from tkinter import ttk
if __name__ == '__main__':
pass
win = tkinter.Tk() # 窗口
win.title('南風丶輕語') # 標題
screenwidth = win.winfo_screenwidth() # 屏幕寬度
screenheight = win.winfo_screenheight() # 屏幕高度
width = 1000
height = 500
x = int((screenwidth - width) / 2)
y = int((screenheight - height) / 2)
win.geometry('{}x{}+{}+{}'.format(width, height, x, y)) # 大小以及位置
columns = ['學號', '姓名', '性別', '出生年月', '籍貫', '班級']
table = ttk.Treeview(
master=win, # 父容器
height=10, # 表格顯示的行數,height行
columns=columns, # 顯示的列
show='headings', # 隱藏首列
)
table.heading(column='學號', text='學號', anchor='w',
command=lambda: print('學號')) # 定義表頭
table.heading('姓名', text='姓名', ) # 定義表頭
table.heading('性別', text='性別', ) # 定義表頭
table.heading('出生年月', text='出生年月', ) # 定義表頭
table.heading('籍貫', text='籍貫', ) # 定義表頭
table.heading('班級', text='班級', ) # 定義表頭
table.column('學號', width=100, minwidth=100, anchor=S, ) # 定義列
table.column('姓名', width=150, minwidth=100, anchor=S) # 定義列
table.column('性別', width=50, minwidth=50, anchor=S) # 定義列
table.column('出生年月', width=150, minwidth=100, anchor=S) # 定義列
table.column('籍貫', width=150, minwidth=100, anchor=S) # 定義列
table.column('班級', width=150, minwidth=100, anchor=S) # 定義列
table.pack(pady=20)
win.mainloop()
運行

2、插入數據到表格中
- 常用參數意義
①table.insert('', END, values=data) # 添加數據到末尾
# -*- encoding=utf-8 -*-
import tkinter
from tkinter import *
from tkinter import messagebox
from tkinter import ttk
def insert():
# 插入數據
info = [
['1001', '李華', '男', '2014-01-25', '廣東', '計算5班', ],
['1002', '小米', '男', '2015-11-08', '深圳', '計算5班', ],
['1003', '劉亮', '男', '2015-09-12', '福建', '計算5班', ],
['1004', '白鴿', '女', '2016-04-01', '湖南', '計算5班', ],
]
for index, data in enumerate(info):
table.insert('', END, values=data) # 添加數據到末尾
if __name__ == '__main__':
pass
win = tkinter.Tk() # 窗口
win.title('南風丶輕語') # 標題
screenwidth = win.winfo_screenwidth() # 屏幕寬度
screenheight = win.winfo_screenheight() # 屏幕高度
width = 1000
height = 500
x = int((screenwidth - width) / 2)
y = int((screenheight - height) / 2)
win.geometry('{}x{}+{}+{}'.format(width, height, x, y)) # 大小以及位置
tabel_frame = tkinter.Frame(win)
tabel_frame.pack()
xscroll = Scrollbar(tabel_frame, orient=HORIZONTAL)
yscroll = Scrollbar(tabel_frame, orient=VERTICAL)
columns = ['學號', '姓名', '性別', '出生年月', '籍貫', '班級']
table = ttk.Treeview(
master=tabel_frame, # 父容器
height=10, # 表格顯示的行數,height行
columns=columns, # 顯示的列
show='headings', # 隱藏首列
xscrollcommand=xscroll.set, # x軸滾動條
yscrollcommand=yscroll.set, # y軸滾動條
)
for column in columns:
table.heading(column=column, text=column, anchor=CENTER,
command=lambda name=column:
messagebox.showinfo('', '{}描述信息~~~'.format(name))) # 定義表頭
table.column(column=column, width=100, minwidth=100, anchor=CENTER, ) # 定義列
xscroll.config(command=table.xview)
xscroll.pack(side=BOTTOM, fill=X)
yscroll.config(command=table.yview)
yscroll.pack(side=RIGHT, fill=Y)
table.pack(fill=BOTH, expand=True)
insert()
insert()
insert()
insert()
btn_frame = Frame()
btn_frame.pack()
Button(btn_frame, text='添加', bg='yellow', width=20, command=insert).pack()
win.mainloop()

3、刪除表格中的數據
- 常用參數意義
①get_children()#獲取所有對象
②table.delete()#刪除對象
# -*- encoding=utf-8 -*-
import tkinter
from tkinter import *
from tkinter import ttk
def insert():
# 插入數據
info = [
['1001', '李華', '男', '2014-01-25', '廣東', '計算5班', ],
['1002', '小米', '男', '2015-11-08', '深圳', '計算5班', ],
['1003', '劉亮', '男', '2015-09-12', '福建', '計算5班', ],
['1004', '白鴿', '女', '2016-04-01', '湖南', '計算5班', ],
]
for index, data in enumerate(info):
table.insert('', END, values=data) # 添加數據到末尾
def delete():
obj = table.get_children() # 獲取所有對象
for o in obj:
table.delete(o) # 刪除對象
if __name__ == '__main__':
pass
win = tkinter.Tk() # 窗口
win.title('南風丶輕語') # 標題
screenwidth = win.winfo_screenwidth() # 屏幕寬度
screenheight = win.winfo_screenheight() # 屏幕高度
width = 1000
height = 500
x = int((screenwidth - width) / 2)
y = int((screenheight - height) / 2)
win.geometry('{}x{}+{}+{}'.format(width, height, x, y)) # 大小以及位置
columns = ['學號', '姓名', '性別', '出生年月', '籍貫', '班級']
table = ttk.Treeview(
master=win, # 父容器
height=10, # 表格顯示的行數,height行
columns=columns, # 顯示的列
show='headings', # 隱藏首列
)
table.heading(column='學號', text='學號', anchor='w',
command=lambda: print('學號')) # 定義表頭
table.heading('姓名', text='姓名', ) # 定義表頭
table.heading('性別', text='性別', ) # 定義表頭
table.heading('出生年月', text='出生年月', ) # 定義表頭
table.heading('籍貫', text='籍貫', ) # 定義表頭
table.heading('班級', text='班級', ) # 定義表頭
table.column('學號', width=100, minwidth=100, anchor=S, ) # 定義列
table.column('姓名', width=150, minwidth=100, anchor=S) # 定義列
table.column('性別', width=50, minwidth=50, anchor=S) # 定義列
table.column('出生年月', width=150, minwidth=100, anchor=S) # 定義列
table.column('籍貫', width=150, minwidth=100, anchor=S) # 定義列
table.column('班級', width=150, minwidth=100, anchor=S) # 定義列
table.pack(pady=20)
insert()
f = Frame()
f.pack()
Button(f, text='添加', bg='yellow', width=20, command=insert).pack(side=LEFT)
Button(f, text='刪除', bg='pink', width=20, command=delete).pack(side=LEFT)
win.mainloop()

4、添加滾動條
- 常用參數意義
①xscroll = Scrollbar(tabel_frame, orient=HORIZONTAL)#水平滾動條
②yscroll = Scrollbar(tabel_frame, orient=VERTICAL)#垂直滾動條
③xscrollcommand=xscroll.set, # table綁定x軸滾動條事件
④yscrollcommand=yscroll.set, # table綁定y軸滾動條事件
⑤xscroll.config(command=table.xview)#水平滾動條綁定table的x軸事件
⑥xscroll.pack(side=BOTTOM, fill=X)#放置水平滾動條,放在最下面
⑦yscroll.config(command=table.yview)#垂直滾動條綁定table的y軸事件
⑧yscroll.pack(side=RIGHT, fill=Y)#垂直滾動條,放置在最右邊
⑨table.pack(fill=BOTH, expand=True)#放置table,必須寫在放置水平和垂直滾動條之后
# -*- encoding=utf-8 -*-
import tkinter
from tkinter import *
from tkinter import messagebox
from tkinter import ttk
def insert():
# 插入數據
info = [
['1001', '李華', '男', '2014-01-25', '廣東', '計算5班', ],
['1002', '小米', '男', '2015-11-08', '深圳', '計算5班', ],
['1003', '劉亮', '男', '2015-09-12', '福建', '計算5班', ],
['1004', '白鴿', '女', '2016-04-01', '湖南', '計算5班', ],
]
for index, data in enumerate(info):
table.insert('', END, values=data) # 添加數據到末尾
if __name__ == '__main__':
pass
win = tkinter.Tk() # 窗口
win.title('南風丶輕語') # 標題
screenwidth = win.winfo_screenwidth() # 屏幕寬度
screenheight = win.winfo_screenheight() # 屏幕高度
width = 1000
height = 500
x = int((screenwidth - width) / 2)
y = int((screenheight - height) / 2)
win.geometry('{}x{}+{}+{}'.format(width, height, x, y)) # 大小以及位置
tabel_frame = tkinter.Frame(win)
tabel_frame.pack()
xscroll = Scrollbar(tabel_frame, orient=HORIZONTAL)
yscroll = Scrollbar(tabel_frame, orient=VERTICAL)
columns = ['學號', '姓名', '性別', '出生年月', '籍貫', '班級']
table = ttk.Treeview(
master=tabel_frame, # 父容器
height=10, # 表格顯示的行數,height行
columns=columns, # 顯示的列
show='headings', # 隱藏首列
xscrollcommand=xscroll.set, # x軸滾動條
yscrollcommand=yscroll.set, # y軸滾動條
)
for column in columns:
table.heading(column=column, text=column, anchor=CENTER,
command=lambda name=column:
messagebox.showinfo('', '{}描述信息~~~'.format(name))) # 定義表頭
table.column(column=column, width=100, minwidth=100, anchor=CENTER, ) # 定義列
xscroll.config(command=table.xview)
xscroll.pack(side=BOTTOM, fill=X)
yscroll.config(command=table.yview)
yscroll.pack(side=RIGHT, fill=Y)
table.pack(fill=BOTH, expand=True)
insert()
insert()
insert()
insert()
btn_frame = Frame()
btn_frame.pack()
Button(btn_frame, text='添加', bg='yellow', width=20, command=insert).pack()
win.mainloop()

- 易錯點
①command=lambda name=column:messagebox.showinfo('', '{}描述信息~~~'.format(name)) 正確綁定事件
點擊表頭后,顯示對應表頭的描述信息

command=lambda :messagebox.showinfo('', '{}描述信息~~~'.format(column)) 錯誤綁定事件
點擊表頭后每次顯示的都是描述班級信息(列表最后一個元素)

區別在於正確寫法給定了形參,每個形參使用for循環給的局部變量column的值,每次都是不一樣的,而錯誤寫法給定的是for循環中的局部變量column,for循環結束后,局部變量column就是最后一個列表的值,因此輸出都是列表最后一個值

