Set Colors In Pandastable
I am using python3.7 to create a table in a tkinter-window. For this table I would like to highlight certain cells/rows/columns. But I am just able to highlight columns via the sta
Solution 1:
I found this question searching answer for similar problem.
After digging in source code of setRowColors I found it can set every cell separatelly so you have to select columns using cols=
because as default it uses cols=None
pt.setRowColors(rows=2, clr='blue', cols=[1,3,5])
or you have to use "all"
to select all columns
pt.setRowColors(rows=1, clr='green', cols='all')
As for pt.rowcolors
- at start it is empty DataFrame
without columns and rows and it has problem to set color correctly but if it used after setRowColors
then it works correctly
pt.setRowColors(rows=1, clr='green', cols='all')
pt.rowcolors['date'] ='green'
pt.rowcolors['label'][5] ='#dcf1fc'
Eventually you would have to manually create columns and rows rowcolors
at start.
import pandas as pd
import numpy as np
for c in df.columns:
if c not in pt.rowcolors.columns:
pt.rowcolors[c] = pd.Series(np.nan,index=df.index)
from tkinter import *
from pandastable import Table, TableModel
import pandas as pd
import numpy as np
classMainClass(Frame):
def__init__(self, parent=None):
self.parent = parent
Frame.__init__(self)
self.main = self.master
self.main.geometry('600x400+200+100')
self.main.title('Overview trading pairs')
f = Frame(self.main)
f.pack(fill=BOTH,expand=1)
df = TableModel.getSampleData()
names = list(df.columns)
pt = Table(f, dataframe=df, showtoolbar=0, showstatusbar=0)
pt.show()
pt.columncolors[names[4]] = '#dcf1fc'
pt.columncolors['label'] = 'red'print('--- rowcolors ---')
print(pt.rowcolors) # empty #pt.rowcolors['label'] = '#dcf1fc' # error #for c in df.columns:# if c not in pt.rowcolors.columns:# pt.rowcolors[c] = pd.Series(np.nan,index=df.index)
pt.setRowColors(rows=1, clr='green', cols='all')
pt.setRowColors(rows=2, clr='blue', cols=[1,3,5])
pt.rowcolors['date'] = 'green'#pt.rowcolors[ ['date','label'] ] = 'green'
pt.rowcolors['label'][5] = '#dcf1fc'
pt.rowcolors['label'][5,10] = '#dcf1fc'print('--- rowcolors ---')
print(pt.rowcolors)
app = MainClass()
app.mainloop()
Post a Comment for "Set Colors In Pandastable"