我目前正在使用一种功能,以电子表格样式格式显示 Pandas 数据框。我希望能够添加一些功能来根据其内容格式化Treeview的各个单元格,例如如果它们包含子字符串“X”,或者它们的值大于Y。

当前实现的更新功能如下:

   def updateTree(self, dataframe):
    '''
    Updates the treeview with the data in the dataframe
    parameter
    '''
    #Remove any nan values which may have appeared in the dataframe parameter
    df = dataframe.replace(np.nan,'', regex=True)

    #Currently displayed data
    self.treesubsetdata = dataframe

    #Remove existing items
    for item in self.tree.get_children(): self.tree.delete(item)
    #Recreate from scratch the columns based on the passed dataframe
    self.tree.config(columns= [])
    self.tree.config(columns= list(dataframe.columns))

    #Ensure all columns are considered strings and write column headers
    for col in dataframe.columns:
        self.tree.heading(col,text=str(col))

    #Get number of rows and columns in the imported script
    self.rows,self.cols = dataframe.shape

    #Populate data in the treeview
    for row in dataframe.itertuples():
        self.tree.insert('', 'end',values = tuple(row[1:]))

    #Minimise first column
    self.tree.column('#0',width=0)
    self.tree.update()

任何人都可以确认您实际上可以在Treview中编辑单个单元格吗?

如果是,是否有关于如何实现的想法?

最佳答案

无法在Treeview中设置单个单元格的样式。只有整行可以使用tag属性。

如果您只想要一个值表,那么我建议您仅使用ttk.Label小部件,您可以采用多种方式对其进行格式化。例如:

import Tkinter as tk
import ttk
import pandas as pd
from random import randrange

PADDING = dict(padx=3, pady=3)
class GridView(tk.Frame):
    def __init__(self, master=None, **kwargs):
        tk.Frame.__init__(self, master, **kwargs)
        self.labels = []
        style = ttk.Style()
        style.configure("red.TLabel", background='red')
        style.configure("green.TLabel", background='green')
        style.configure("header.TLabel", font = '-weight bold')

    def set(self, df):
        self.clear()
        for col, name in enumerate(df.columns):
            lbl = ttk.Label(self, text=name, style='header.TLabel')
            lbl.grid(row=0, column=col, **PADDING)
            self.labels.append(lbl)

        for row, values in enumerate(df.itertuples(), 1):
            for col, value in enumerate(values[1:]):
                lbl = ttk.Label(self, text=value, style=self.get_style(value))
                lbl.grid(row=row, column=col, **PADDING)
                self.labels.append(lbl)

    @staticmethod
    def get_style(value):
        if value > 70:
            return "red.TLabel"
        elif value < 30:
            return "green.TLabel"
        else:
            return None

    def clear(self):
        for lbl in self.labels:
            lbl.grid_forget()
        self.labels = []

class GUI(tk.Frame):
    def __init__(self, master=None, **kwargs):
        tk.Frame.__init__(self, master, **kwargs)
        self.table = GridView(self)
        self.table.pack()
        btn = ttk.Button(self, text="populate", command=self.populate)
        btn.pack()
        btn = ttk.Button(self, text="clear", command=self.table.clear)
        btn.pack()

    def populate(self):
        self.table.set(new_rand_df())

def main():
    root = tk.Tk()
    win = GUI(root)
    win.pack()
    root.mainloop()

def new_rand_df():
    width = 5
    height = 5
    return pd.DataFrame([[randrange(100) for _ in range(width)] for _ in range(height)], columns = list('abcdefghijklmnopqrstuvwxyz'[:width]))

if __name__ == '__main__':
    main()

关于python - 在tkinter ttk treeview中格式化单个单元格/项目而不是整个行,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42668115/

10-13 01:18