首页 > 解决方案 > 如何突出显示 csv 数据的行差异?

问题描述

我有这张桌子:

在此处输入图像描述

我使用以下代码创建了带有树视图的 tkinter 表:

from tkinter import *
import tkinter.ttk as ttk
import csv

root = Tk()
root.title("Python - Import CSV File To Tkinter Table")
width = 500
height = 400
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
x = (screen_width / 2) - (width / 2)
y = (screen_height / 2) - (height / 2)
root.geometry("%dx%d+%d+%d" % (width, height, x, y))
root.resizable(0, 0)

TableMargin = Frame(root, width=500)
TableMargin.pack(side=TOP)

scrollbarx = Scrollbar(TableMargin, orient=HORIZONTAL)
scrollbary = Scrollbar(TableMargin, orient=VERTICAL)
tree = ttk.Treeview(TableMargin, columns=("Var1", "Var2", "Var3",'Var4'), height=400, selectmode="extended",
                    yscrollcommand=scrollbary.set, xscrollcommand=scrollbarx.set)
scrollbary.config(command=tree.yview)
scrollbary.pack(side=RIGHT, fill=Y)
scrollbarx.config(command=tree.xview)
scrollbarx.pack(side=BOTTOM, fill=X)
tree.heading('Var1', text="Var1", anchor=W)
tree.heading('Var2', text="Var2", anchor=W)
tree.heading('Var3', text="Var3", anchor=W)
tree.heading('Var4', text="Var4", anchor=W)
tree.column('#0', stretch=NO, minwidth=0, width=0)
tree.column('#1', stretch=NO, minwidth=0, width=120)
tree.column('#2', stretch=NO, minwidth=0, width=120)
tree.column('#3', stretch=NO, minwidth=0, width=120)
tree.column('#3', stretch=NO, minwidth=0, width=120)
tree.pack()
with open('./dati/esempio.csv') as f:
  reader = csv.DictReader(f, delimiter=',')
  for row in reader:
    emp_id = row['Var1']
    name = row['Var2']
    dt = row['Var3']
    ti = row['Var4']
    tree.insert("", 5, values=(emp_id, name, dt,ti))
root.mainloop()

如何突出显示 python Tkinter 中的行数据差异,如上表所示?每一行与前一行。

标签: pythontkinter

解决方案


推荐阅读