PySimpleGUI/DemoPrograms/Demo_Table_Element.py

73 lines
2.5 KiB
Python
Raw Permalink Normal View History

2018-09-27 20:24:09 +00:00
#!/usr/bin/env python
import PySimpleGUI as sg
import random
import string
2019-12-24 23:52:47 +00:00
"""
Basic use of the Table Element
Copyright 2022 PySimpleGUI
2019-12-24 23:52:47 +00:00
"""
2019-12-24 23:52:47 +00:00
# ------ Some functions to help generate data for the table ------
def word():
return ''.join(random.choice(string.ascii_lowercase) for i in range(10))
def number(max_val=1000):
return random.randint(0, max_val)
def make_table(num_rows, num_cols):
data = [[j for j in range(num_cols)] for i in range(num_rows)]
2019-12-24 23:52:47 +00:00
data[0] = [word() for __ in range(num_cols)]
for i in range(1, num_rows):
data[i] = [word(), *[number() for i in range(num_cols - 1)]]
return data
2019-12-24 23:52:47 +00:00
# ------ Make the Table Data ------
data = make_table(num_rows=15, num_cols=6)
headings = [str(data[0][x])+' ..' for x in range(len(data[0]))]
2019-12-24 23:52:47 +00:00
# ------ Window Layout ------
layout = [[sg.Table(values=data[1:][:], headings=headings, max_col_width=25,
auto_size_columns=True,
# cols_justification=('left','center','right','c', 'l', 'bad'), # Added on GitHub only as of June 2022
display_row_numbers=True,
justification='center',
num_rows=20,
alternating_row_color='lightblue',
2019-12-24 23:52:47 +00:00
key='-TABLE-',
selected_row_colors='red on yellow',
enable_events=True,
expand_x=False,
expand_y=True,
vertical_scroll_only=False,
enable_click_events=True, # Comment out to not enable header and other clicks
tooltip='This is a table')],
2019-12-24 23:52:47 +00:00
[sg.Button('Read'), sg.Button('Double'), sg.Button('Change Colors')],
[sg.Text('Read = read which rows are selected')],
[sg.Text('Double = double the amount of data in the table')],
[sg.Text('Change Colors = Changes the colors of rows 8 and 9'), sg.Sizegrip()]]
2018-10-29 00:01:03 +00:00
2019-12-24 23:52:47 +00:00
# ------ Create Window ------
window = sg.Window('The Table Element', layout,
# ttk_theme='clam',
# font='Helvetica 25',
resizable=True
)
2018-10-29 00:01:03 +00:00
2019-12-24 23:52:47 +00:00
# ------ Event Loop ------
2018-10-29 00:01:03 +00:00
while True:
event, values = window.read()
print(event, values)
if event == sg.WIN_CLOSED:
2018-10-29 00:01:03 +00:00
break
if event == 'Double':
for i in range(1,len(data)):
2018-10-29 00:01:03 +00:00
data.append(data[i])
window['-TABLE-'].update(values=data[1:][:])
2019-12-24 23:52:47 +00:00
elif event == 'Change Colors':
window['-TABLE-'].update(row_colors=((8, 'white', 'red'), (9, 'green')))
window.close()