Python使用Treeview制作表格

Python使用Treeview制作表格,第1张

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

"""
需安装的库文件:
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()

效果如下:

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

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/920884.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-16
下一篇 2022-05-16

发表评论

登录后才能评论

评论列表(0条)

保存