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 string
# ------------------ Create a fake table ------------------
class Fake():
@classmethod
@ -30,22 +31,26 @@ data = make_table(num_rows=15, num_cols=6)
# sg.SetOptions(element_padding=(0,0))
headings = [data[0][x] for x in range(len(data[0]))]
layout = [[sg.Table(values=data[1:][:], headings=headings, max_col_width=25,
auto_size_columns=True, display_row_numbers=True, justification='right', num_rows=20, alternating_row_color='lightblue', key='_table_')],
[sg.Button('Read'), sg.Button('Double')],
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='blue', key='_table_', tooltip='This is a table')],
[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')]]
window = sg.Window('Table', grab_anywhere=False, resizable=True).Layout(layout)
while True:
event, values = window.Read()
print(event, values)
if event is None:
break
if event == 'Double':
for i in range(len(data)):
data.append(data[i])
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)
window.Close()
sys.exit(69)

View File

@ -4405,7 +4405,7 @@ class Table(Element):
self.RowHeaderText = 'Row'
self.RightClickMenu = right_click_menu
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,
size=size, pad=pad, key=key, tooltip=tooltip, visible=visible)
return
@ -4419,21 +4419,32 @@ class Table(Element):
:param visible: (bool) if True then will be visible
: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 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:
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()
for i in children:
self.TKTreeview.detach(i)
self.TKTreeview.delete(i)
children = self.TKTreeview.get_children()
# self.TKTreeview.delete(*self.TKTreeview.get_children())
self.tree_ids =[]
for i, value in enumerate(values):
if self.DisplayRowNumbers:
value = [i + self.StartingRowNumber] + value
id = self.TKTreeview.insert('', 'end', text=i, iid=i + 1, values=value, tag=i % 2)
if self.AlternatingRowColor is not None:
self.TKTreeview.tag_configure(1, background=self.AlternatingRowColor)
id = self.TKTreeview.insert('', 'end', text=value, iid=i + 1, values=value, tag=i)
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')
self.tree_ids.append(id)
self.Values = values
self.SelectedRows = []
if visible is False:
@ -8116,6 +8127,7 @@ def PackFormIntoFrame(form, containing_frame, toplevel_form):
if element.DisplayRowNumbers:
value = [i + element.StartingRowNumber] + value
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
for row in range(0, len(element.Values), 2):
treeview.tag_configure(row, background=element.AlternatingRowColor)