PySimpleGUI/PySimpleGUIWeb/Demo Programs/Web_Table_Element.py

44 lines
1.5 KiB
Python
Raw Normal View History

2019-03-15 02:28:49 +00:00
import PySimpleGUIWeb as sg
import random
import string
# Example with Table element
2019-03-15 14:03:43 +00:00
def word():
return ''.join(random.choice(string.ascii_lowercase) for i in range(10))
2019-03-15 14:03:43 +00:00
def number(max_val=1000):
return random.randint(0,max_val)
2019-03-15 02:28:49 +00:00
def make_table(num_rows, num_cols):
data = [[j for j in range(num_cols)] for i in range(num_rows)]
data[0] = [word() for _ in range(num_cols)]
2019-03-15 02:28:49 +00:00
for i in range(1, num_rows):
data[i] = [word(), *[number() for i in range(num_cols - 1)]]
2019-03-15 02:28:49 +00:00
return data
table_data = make_table(num_rows=15, num_cols=6)
# ------------------ Create a window layout ------------------
layout = [[sg.Table(values=table_data, enable_events=True,
display_row_numbers=True, font='Courier 14',
row_header_text='Row #', key='_table_', text_color='red')],
2019-03-15 02:28:49 +00:00
[sg.Button('Exit')],
[sg.Text('Selected rows = '), sg.Text('', size=(30,1), key='_selected_rows_')],
[sg.Text('Selected value = '), sg.Text('', size=(30,1), key='_selected_value_')]]
2019-03-15 02:28:49 +00:00
# ------------------ Create the window ------------------
window = sg.Window('Table Element Example', layout)
2019-03-15 02:28:49 +00:00
# ------------------ The Event Loop ------------------
while True:
event, values = window.read()
2019-03-15 14:03:43 +00:00
print(event, values)
2019-03-15 02:28:49 +00:00
if event in (None, 'Exit'):
break
window['_selected_rows_'].update(values['_table_'])
window['_selected_value_'].update(window['_table_'].SelectedItem)
2019-03-15 02:28:49 +00:00
# ------------------ User closed window so exit ------------------
window.close()