2018-09-27 20:24:09 +00:00
|
|
|
#!/usr/bin/env python
|
2019-10-23 20:10:03 +00:00
|
|
|
import PySimpleGUI as sg
|
2018-09-11 12:38:38 +00:00
|
|
|
import csv
|
2018-09-27 20:24:09 +00:00
|
|
|
|
2018-09-07 03:17:40 +00:00
|
|
|
|
|
|
|
def TableSimulation():
|
|
|
|
"""
|
|
|
|
Display data in a table format
|
|
|
|
"""
|
2019-10-23 20:10:03 +00:00
|
|
|
sg.set_options(element_padding=(0, 0))
|
2018-09-11 12:38:38 +00:00
|
|
|
|
|
|
|
menu_def = [['File', ['Open', 'Save', 'Exit']],
|
2019-10-23 20:10:03 +00:00
|
|
|
['Edit', ['Paste', ['Special', 'Normal', ], 'Undo'], ],
|
|
|
|
['Help', 'About...'], ]
|
2018-09-11 12:38:38 +00:00
|
|
|
|
2018-10-29 00:01:03 +00:00
|
|
|
MAX_ROWS = 20
|
2018-09-11 12:38:38 +00:00
|
|
|
MAX_COL = 10
|
2019-12-24 23:52:47 +00:00
|
|
|
|
|
|
|
columm_layout = [[sg.Text(str(i), size=(4, 1), justification='right')] + [sg.Input(size=(10, 1), pad=(
|
|
|
|
1, 1), justification='right', key=(i, j)) for j in range(MAX_COL)] for i in range(MAX_ROWS)]
|
|
|
|
|
2018-09-11 12:38:38 +00:00
|
|
|
|
2019-10-23 20:10:03 +00:00
|
|
|
layout = [[sg.Menu(menu_def)],
|
|
|
|
[sg.Text('Table Using Combos and Input Elements', font='Any 18')],
|
|
|
|
[sg.Text('Type in a row, column and value. The form will update the values in realtime as you type'),
|
|
|
|
sg.Input(key='inputrow', justification='right', size=(8, 1), pad=(1, 1)),
|
|
|
|
sg.Input(key='inputcol', size=(8, 1), pad=(1, 1), justification='right'),
|
|
|
|
sg.Input(key='value', size=(8, 1), pad=(1, 1), justification='right')],
|
|
|
|
[sg.Col(columm_layout, size=(800, 600), scrollable=True)]]
|
2018-09-07 03:17:40 +00:00
|
|
|
|
2019-10-23 20:10:03 +00:00
|
|
|
window = sg.Window('Table', layout, return_keyboard_events=True)
|
2018-09-07 03:17:40 +00:00
|
|
|
|
2018-09-07 14:32:05 +00:00
|
|
|
while True:
|
2019-10-23 20:10:03 +00:00
|
|
|
event, values = window.read()
|
2018-09-11 12:38:38 +00:00
|
|
|
# --- Process buttons --- #
|
2020-05-07 10:22:59 +00:00
|
|
|
if event in (sg.WIN_CLOSED, 'Exit'):
|
2018-09-07 14:32:05 +00:00
|
|
|
break
|
2018-10-15 20:07:23 +00:00
|
|
|
elif event == 'About...':
|
2019-10-23 20:10:03 +00:00
|
|
|
sg.popup('Demo of table capabilities')
|
2018-10-15 20:07:23 +00:00
|
|
|
elif event == 'Open':
|
2019-10-23 20:10:03 +00:00
|
|
|
filename = sg.popup_get_file(
|
|
|
|
'filename to open', no_window=True, file_types=(("CSV Files", "*.csv"),))
|
2018-09-11 12:38:38 +00:00
|
|
|
# --- populate table with file contents --- #
|
|
|
|
if filename is not None:
|
|
|
|
with open(filename, "r") as infile:
|
|
|
|
reader = csv.reader(infile)
|
|
|
|
try:
|
2019-10-23 20:10:03 +00:00
|
|
|
# read everything else into a list of rows
|
|
|
|
data = list(reader)
|
2018-09-11 12:38:38 +00:00
|
|
|
except:
|
2019-10-23 20:10:03 +00:00
|
|
|
sg.popup_error('Error reading file')
|
2018-09-11 12:38:38 +00:00
|
|
|
continue
|
|
|
|
# clear the table
|
2019-10-23 20:10:03 +00:00
|
|
|
[window[(i, j)].update('') for j in range(MAX_COL)
|
|
|
|
for i in range(MAX_ROWS)]
|
2018-09-11 12:38:38 +00:00
|
|
|
|
|
|
|
for i, row in enumerate(data):
|
|
|
|
for j, item in enumerate(row):
|
2019-10-23 20:10:03 +00:00
|
|
|
location = (i, j)
|
2018-09-11 12:38:38 +00:00
|
|
|
try: # try the best we can at reading and filling the table
|
2019-10-23 20:10:03 +00:00
|
|
|
target_element = window[location]
|
2018-09-11 12:38:38 +00:00
|
|
|
new_value = item
|
|
|
|
if target_element is not None and new_value != '':
|
2019-10-23 20:10:03 +00:00
|
|
|
target_element.update(new_value)
|
2018-09-11 12:38:38 +00:00
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
|
|
|
# if a valid table location entered, change that location's value
|
2018-09-07 14:32:05 +00:00
|
|
|
try:
|
|
|
|
location = (int(values['inputrow']), int(values['inputcol']))
|
2019-10-23 20:10:03 +00:00
|
|
|
target_element = window[location]
|
2018-09-07 14:32:05 +00:00
|
|
|
new_value = values['value']
|
|
|
|
if target_element is not None and new_value != '':
|
2019-10-23 20:10:03 +00:00
|
|
|
target_element.update(new_value)
|
2018-09-07 14:32:05 +00:00
|
|
|
except:
|
|
|
|
pass
|
2018-09-07 03:17:40 +00:00
|
|
|
|
2019-10-23 20:10:03 +00:00
|
|
|
window.close()
|
|
|
|
|
|
|
|
|
|
|
|
TableSimulation()
|