Support for Table Updates for colors (individual row colors and alternating row color)

This commit is contained in:
MikeTheWatchGuy 2019-07-19 22:22:49 -04:00
parent 6c620efc83
commit b49e17d665
2 changed files with 27 additions and 10 deletions

View File

@ -8,6 +8,7 @@ else:
import random import random
import string import string
# ------------------ Create a fake table ------------------ # ------------------ Create a fake table ------------------
class Fake(): class Fake():
@classmethod @classmethod
@ -30,22 +31,26 @@ data = make_table(num_rows=15, num_cols=6)
# sg.SetOptions(element_padding=(0,0)) # sg.SetOptions(element_padding=(0,0))
headings = [data[0][x] for x in range(len(data[0]))] headings = [data[0][x] for x in range(len(data[0]))]
layout = [[sg.Table(values=data[1:][:], headings=headings, max_col_width=25, layout = [[sg.Table(values=data[1:][:], headings=headings, max_col_width=25, background_color='lightblue',
auto_size_columns=True, display_row_numbers=True, justification='right', num_rows=20, alternating_row_color='lightblue', key='_table_')], auto_size_columns=True, display_row_numbers=True, justification='right', num_rows=20, alternating_row_color='blue', key='_table_', tooltip='This is a table')],
[sg.Button('Read'), sg.Button('Double')], [sg.Button('Read'), sg.Button('Double'), sg.Button('Update')],
[sg.T('Read = read which rows are selected')],[sg.T('Double = double the amount of data in the table')]] [sg.T('Read = read which rows are selected')],[sg.T('Double = double the amount of data in the table')]]
window = sg.Window('Table', grab_anywhere=False, resizable=True).Layout(layout) window = sg.Window('Table', grab_anywhere=False, resizable=True).Layout(layout)
while True: while True:
event, values = window.Read() event, values = window.Read()
print(event, values)
if event is None: if event is None:
break break
if event == 'Double': if event == 'Double':
for i in range(len(data)): for i in range(len(data)):
data.append(data[i]) data.append(data[i])
window.FindElement('_table_').Update(values = data) window.FindElement('_table_').Update(values = data)
sg.Popup(event, values) elif event == 'Update':
window.FindElement('_table_').Update( row_colors=((8,'red'), (9,'black')))
# sg.Popup(event, values)
# print(event, values) # print(event, values)
window.Close() window.Close()
sys.exit(69) sys.exit(69)

View File

@ -4405,7 +4405,7 @@ class Table(Element):
self.RowHeaderText = 'Row' self.RowHeaderText = 'Row'
self.RightClickMenu = right_click_menu self.RightClickMenu = right_click_menu
self.RowColors = row_colors self.RowColors = row_colors
self.tree_ids = [] # ids returned when inserting items into table - will use to delete colors
super().__init__(ELEM_TYPE_TABLE, text_color=text_color, background_color=background_color, font=font, super().__init__(ELEM_TYPE_TABLE, text_color=text_color, background_color=background_color, font=font,
size=size, pad=pad, key=key, tooltip=tooltip, visible=visible) size=size, pad=pad, key=key, tooltip=tooltip, visible=visible)
return return
@ -4419,21 +4419,32 @@ class Table(Element):
:param visible: (bool) if True then will be visible :param visible: (bool) if True then will be visible
:param select_rows: List[int] List of rows to select as if user did :param select_rows: List[int] List of rows to select as if user did
:param alternating_row_color: (str) the color to make every other row :param alternating_row_color: (str) the color to make every other row
:param row_colors: List[Tuple[int, str]] list of tuples of (row, color). Changes the colors of listed rows to the color provided :param row_colors: List[Union[Tuple[int, str], Tuple[Int, str, str]] list of tuples of (row, background color) OR (row, foreground color, background color). Changes the colors of listed rows to the color(s) provided (note the optional foreground color)
""" """
if values is not None: if values is not None:
for id in self.tree_ids:
self.TKTreeview.item(id, tags=())
if self.BackgroundColor is not None and self.BackgroundColor != COLOR_SYSTEM_DEFAULT:
self.TKTreeview.tag_configure(id, background=self.BackgroundColor)
else:
self.TKTreeview.tag_configure(id, background='#FFFFFF')
children = self.TKTreeview.get_children() children = self.TKTreeview.get_children()
for i in children: for i in children:
self.TKTreeview.detach(i) self.TKTreeview.detach(i)
self.TKTreeview.delete(i) self.TKTreeview.delete(i)
children = self.TKTreeview.get_children() children = self.TKTreeview.get_children()
# self.TKTreeview.delete(*self.TKTreeview.get_children())
self.tree_ids =[]
for i, value in enumerate(values): for i, value in enumerate(values):
if self.DisplayRowNumbers: if self.DisplayRowNumbers:
value = [i + self.StartingRowNumber] + value value = [i + self.StartingRowNumber] + value
id = self.TKTreeview.insert('', 'end', text=i, iid=i + 1, values=value, tag=i % 2) id = self.TKTreeview.insert('', 'end', text=value, iid=i + 1, values=value, tag=i)
if self.AlternatingRowColor is not None: if self.BackgroundColor is not None and self.BackgroundColor != COLOR_SYSTEM_DEFAULT:
self.TKTreeview.tag_configure(1, background=self.AlternatingRowColor) self.TKTreeview.tag_configure(id, background=self.BackgroundColor)
else:
self.TKTreeview.tag_configure(id, background='#FFFFFF')
self.tree_ids.append(id)
self.Values = values self.Values = values
self.SelectedRows = [] self.SelectedRows = []
if visible is False: if visible is False:
@ -8116,6 +8127,7 @@ def PackFormIntoFrame(form, containing_frame, toplevel_form):
if element.DisplayRowNumbers: if element.DisplayRowNumbers:
value = [i + element.StartingRowNumber] + value value = [i + element.StartingRowNumber] + value
id = treeview.insert('', 'end', text=value, iid=i + 1, values=value, tag=i) id = treeview.insert('', 'end', text=value, iid=i + 1, values=value, tag=i)
element.tree_ids.append(id)
if element.AlternatingRowColor is not None: # alternating colors if element.AlternatingRowColor is not None: # alternating colors
for row in range(0, len(element.Values), 2): for row in range(0, len(element.Values), 2):
treeview.tag_configure(row, background=element.AlternatingRowColor) treeview.tag_configure(row, background=element.AlternatingRowColor)