自由,分享
pandastable,插入数据和设置底色

image-1691165015426

import tkinter as tk
from pandastable import Table, TableModel
import pandas as pd

root = tk.Tk()

# 创建一个Pandas表格
df1 = pd.DataFrame({'a列': [''],'b列': [""],'c列': [''],'d列': [''],'e列': ['']})
ui=tk.Frame(root)
ui.pack()
table = Table(ui, dataframe=df1)
table.show()

flag=True
def insert_data():
    global flag
    table.model.df.loc[len(table.model.df.index)] = ["方法都是", "是否是", "算法", "是否是", "是否是否"]
    if flag:
        table.model.df=table.model.df.drop(index=0)
        flag=False
    table.redraw()
    table.movetoSelection(row=(len(table.model.df.index)))
    table.insertRow()

def set_last_row_red():
    table.setRowColors(rows=len(table.model.df.index)-2, clr='red',cols="all")

# 创建按钮并绑定事件
insert_button = tk.Button(root, text='插入数据', command=insert_data)
insert_button.pack()

set_red_button = tk.Button(root, text='设置最后一行为红色', command=set_last_row_red)
set_red_button.pack()

root.mainloop()