First post and this is my first python3 project. I got stuck with the question how to change the textcolor in a pandas table (pt) based on a database value? The color of each row is stored in the db attribute 'Row_colour' and it's part of the SQL-query down in the code. The pt documentation lists a method called .textcolor() but it applies for all rows and columns in the table. An alternative to change the textcolor is to change the background of each row. This requires a loop through the table or dataframe which is very slow and takes ages to set the background color. Using a pre-defined mask to set the color by '.setColorByMask()' is imho no option since there is no condition to apply.
So any help would be appreciated.
My code snippet is as follows:
class Root_app(ctk.CTk):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.conf_root_window()
global row_count
row_count = self.create_main_sheet()
self.toplevel_window = None
def create_main_sheet(self):
f = Frame(self)
f.pack(fill=BOTH, expand=1)
conn = sqlite3.connect("Lenses.sqlite")
all_lenses = ("Select "
"l.ID ID, "
"COALESCE(LensLabel,'') Lens, "
"COALESCE(FocalLength,'') Focal_Length, "
"COALESCE(FocalLength_sort,'') Focal_Length, "
# ...
"COALESCE(l.Row_colour,'') 'Row_colour' "
"from Lenses l left join Makers ma on l.MakerId=ma.ID "
"left join Mounts ms on l.MountID=ms.ID "
"order by l.FocalLength_sort, MaxAperture"
)
global df
df = pd.read_sql_query(all_lenses, conn)
global pt
pt = Table(f, dataframe=df, showtoolbar=False, showstatusbar=True,
maxcellwidth=500)
options = {'fontsize': 17, 'cellwidth': 100, 'rowheight': 35}
config.apply_options(options, pt)
pt.columncolors['ID'] = '#dcf1fc'
pt.autoResizeColumns()
pt.show()
row_count = len(df)
pt.rowheader.bind('<Button-1>', handle_left_click)
pt.bind("<Button-1>", handle_left_click)
pt.bind('<Double-Button-1>', handle_double_click)
#pt.getCellCoords.bind('<Button-1>', handle_left_click)
conn.close()
return row_count
if __name__ == '__main__':
root = Root_app()
root.mainloop()