Python tkinter之Treeview(表格)

时间:2024-03-18 07:38:54

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就是最后一个列表的值,因此输出都是列表最后一个值