Python使用Treeview制作表格

本文需要以下的库文件,读者可以按照下方的方法进行安装:

"""
需安装的库文件:
pip install ttkbootstrap

换源网址:
- 豆瓣:http://pypi.douban.com/simple/
- 中科大:https://pypi.mirrors.ustc.edu.cn/simple/
- 清华:https://pypi.tuna.tsinghua.edu.cn/simple

换源安装,例如:pip install ttkbootstrap -i https://pypi.tuna.tsinghua.edu.cn/simple
"""

首先开始制作界面:

import tkinter as tk
import time
import sys
import threading

import ttkbootstrap
from tkinter import Canvas, ttk, scrolledtext, Button, VERTICAL, NS, NSEW, END

window = tk.Tk()
# 设置标题
window.title('实验界面')

# 窗口的位置和大小
sw = window.winfo_screenwidth()

# 得到屏幕宽度
sh = window.winfo_screenheight()

# 得到屏幕高度
ww = 800
wh = 600

# 窗口宽高为500
x = (sw-ww) / 2
y = (sh-wh) / 2
window.geometry("%dx%d+%d+%d" % (ww, wh, x, y))

# 设置窗口是否可以变化长宽,默认可变
window.resizable(width=False, height=False)

window.mainloop()

效果如下:
在这里插入图片描述

添加对应的组件:

import tkinter as tk
import time
import sys
import threading

import ttkbootstrap
from tkinter import Canvas, ttk, scrolledtext, Button, VERTICAL, NS, NSEW, END

window = tk.Tk()
# 设置标题
window.title('实验界面')

# 窗口的位置和大小
sw = window.winfo_screenwidth()

# 得到屏幕宽度
sh = window.winfo_screenheight()

# 得到屏幕高度
ww = 800
wh = 600

# 窗口宽高为500
x = (sw-ww) / 2
y = (sh-wh) / 2
window.geometry("%dx%d+%d+%d" % (ww, wh, x, y))

# 设置窗口是否可以变化长宽,默认可变
window.resizable(width=False, height=False)


canvas = Canvas(window)
# 创建表格
tree_date = ttk.Treeview(canvas,  show='headings', height=20)
canvas.place(x=10, y=10)

# 定义列
tree_date["columns"] = ["name", "age", "weight", "number", "hang"]
tree_date.pack()

# 设置列宽度
tree_date.column("name", width=100)
tree_date.column("age", width=100)
tree_date.column("weight", width=100)
tree_date.column("number", width=100)
tree_date.column("hang", width=100)


# 添加列名
tree_date.heading("name", text="姓名")
tree_date.heading("age", text="年龄")
tree_date.heading("weight", text="体重")
tree_date.heading("number", text="状态")
tree_date.heading("hang", text="行")

vbar = ttk.Scrollbar(canvas, orient=VERTICAL, command=tree_date.yview)
tree_date.configure(yscrollcommand=vbar.set)
tree_date.grid(row=0, column=0, sticky=NSEW)
vbar.grid(row=0, column=1, sticky=NS)

scr_explain = scrolledtext.ScrolledText(window, font=('微软雅黑', 15), width=107, height=10)
scr_explain.place(x=10, y=400)

run_button = Button(window, text="run", font=('微软雅黑', 20), width=10)
run_button.place(x=550, y=100)

exit_button = Button(window, text="exit", font=('微软雅黑', 20), width=10)
exit_button.place(x=550, y=200)

window.mainloop()

效果如下:
在这里插入图片描述

最后填充对应函数:

import tkinter as tk
import time
import sys
import threading

import ttkbootstrap
from tkinter import Canvas, ttk, scrolledtext, Button, VERTICAL, NS, NSEW, END

def tool_exit():
    sys.exit()

def delButton(tree):
    x=tree.get_children()
    for item in x:
        tree.delete(item)

def all_run_form(save_dist: list):
    all_data = save_dist
    num = 0
    delButton(tree_date)
    for data in all_data:
        idd = tree_date.insert('', num, values=tuple(data))
        tree_date.see(idd)
        tree_date.update()
        num += 1


def run_main():
    scr_explain.delete(0.0, END)
    delButton(tree_date)
    all_data = []
    for num in range(1, 21):
        time.sleep(0.5)
        scr_explain.insert(END, "表格加载第{}次!\n".format(num))
        scr_explain.yview_moveto(1)
        data = ["张三", "18", "70kg", "ok", num]
        # 删除后填充再数据
        # all_data.append(data)
        # all_run_form(all_data)

        # 直接在已有数据后填充
        idd = tree_date.insert('', num, values=tuple(data))
        tree_date.see(idd)
        tree_date.update()
        num += 1


def thread_it(func):
    '''将函数打包进线程'''
    # 创建
    t = threading.Thread(target=func)
    # 守护 !!!
    t.setDaemon(True)
    # 启动
    t.start()
    # 阻塞--卡死界面!
    # t.join()

window = tk.Tk()
# 设置标题
window.title('实验界面')

# 窗口的位置和大小
sw = window.winfo_screenwidth()

# 得到屏幕宽度
sh = window.winfo_screenheight()

# 得到屏幕高度
ww = 800
wh = 600

# 窗口宽高为500
x = (sw-ww) / 2
y = (sh-wh) / 2
window.geometry("%dx%d+%d+%d" % (ww, wh, x, y))

# 设置窗口是否可以变化长宽,默认可变
window.resizable(width=False, height=False)

canvas = Canvas(window)
# 创建表格
tree_date = ttk.Treeview(canvas,  show='headings', height=20)
canvas.place(x=10, y=10)

# 定义列
tree_date["columns"] = ["name", "age", "weight", "number", "hang"]
tree_date.pack()

# 设置列宽度
tree_date.column("name", width=100)
tree_date.column("age", width=100)
tree_date.column("weight", width=100)
tree_date.column("number", width=100)
tree_date.column("hang", width=100)


# 添加列名
tree_date.heading("name", text="姓名")
tree_date.heading("age", text="年龄")
tree_date.heading("weight", text="体重")
tree_date.heading("number", text="状态")
tree_date.heading("hang", text="行")


vbar = ttk.Scrollbar(canvas, orient=VERTICAL, command=tree_date.yview)
tree_date.configure(yscrollcommand=vbar.set)
tree_date.grid(row=0, column=0, sticky=NSEW)
vbar.grid(row=0, column=1, sticky=NS)

scr_explain = scrolledtext.ScrolledText(window, font=('微软雅黑', 15), width=107, height=10)
scr_explain.place(x=10, y=400)

run_button = Button(window, text="run", font=('微软雅黑', 20), width=10, command=lambda: thread_it(run_main))
run_button.place(x=550, y=100)

exit_button = Button(window, text="exit", font=('微软雅黑', 20), width=10, command=tool_exit)
exit_button.place(x=550, y=200)


window.mainloop()

效果如下:
在这里插入图片描述
本次的博文写到这里了,欢迎大家的点赞,评论和收藏一波,代码中有错误或纰漏处也欢迎各位指出,我会在第一时间进行修改的。

  • 6
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值