2019-03-15 02:28:49 +00:00
|
|
|
import PySimpleGUIWeb as sg
|
|
|
|
import random
|
|
|
|
import string
|
|
|
|
|
2019-10-30 18:35:01 +00:00
|
|
|
# Example with Table element
|
2019-03-15 14:03:43 +00:00
|
|
|
|
2019-10-30 18:35:01 +00:00
|
|
|
def word():
|
|
|
|
return ''.join(random.choice(string.ascii_lowercase) for i in range(10))
|
2019-03-15 14:03:43 +00:00
|
|
|
|
2019-10-30 18:35:01 +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)]
|
2019-10-30 18:35:01 +00:00
|
|
|
data[0] = [word() for _ in range(num_cols)]
|
2019-03-15 02:28:49 +00:00
|
|
|
for i in range(1, num_rows):
|
2019-10-30 18:35:01 +00:00
|
|
|
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 ------------------
|
2019-10-30 18:35:01 +00:00
|
|
|
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')],
|
2019-10-30 18:35:01 +00:00
|
|
|
[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 ------------------
|
2019-10-30 18:35:01 +00:00
|
|
|
window = sg.Window('Table Element Example', layout)
|
2019-03-15 02:28:49 +00:00
|
|
|
|
|
|
|
# ------------------ The Event Loop ------------------
|
|
|
|
while True:
|
2019-10-30 18:35:01 +00:00
|
|
|
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
|
2019-10-30 18:35:01 +00:00
|
|
|
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 ------------------
|
2019-10-30 18:35:01 +00:00
|
|
|
window.close()
|