2018-09-27 20:24:09 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
import sys
|
2018-09-28 18:57:37 +00:00
|
|
|
if sys.version_info[0] >= 3:
|
2018-09-27 20:24:09 +00:00
|
|
|
import PySimpleGUI as sg
|
2018-09-28 18:57:37 +00:00
|
|
|
else:
|
|
|
|
import PySimpleGUI27 as sg
|
2018-09-20 01:11:45 +00:00
|
|
|
import csv
|
2018-09-28 18:57:37 +00:00
|
|
|
|
2018-09-20 01:11:45 +00:00
|
|
|
def table_example():
|
|
|
|
filename = sg.PopupGetFile('filename to open', no_window=True, file_types=(("CSV Files","*.csv"),))
|
|
|
|
# --- populate table with file contents --- #
|
|
|
|
if filename == '':
|
2018-09-24 22:01:00 +00:00
|
|
|
sys.exit(69)
|
2018-09-20 01:11:45 +00:00
|
|
|
data = []
|
|
|
|
header_list = []
|
|
|
|
button = sg.PopupYesNo('Does this file have column names already?')
|
|
|
|
if filename is not None:
|
|
|
|
with open(filename, "r") as infile:
|
|
|
|
reader = csv.reader(infile)
|
|
|
|
if button == 'Yes':
|
|
|
|
header_list = next(reader)
|
|
|
|
try:
|
|
|
|
data = list(reader) # read everything else into a list of rows
|
|
|
|
if button == 'No':
|
|
|
|
header_list = ['column' + str(x) for x in range(len(data[0]))]
|
|
|
|
except:
|
|
|
|
sg.PopupError('Error reading file')
|
2018-09-24 22:01:00 +00:00
|
|
|
sys.exit(69)
|
2018-09-20 01:11:45 +00:00
|
|
|
sg.SetOptions(element_padding=(0, 0))
|
|
|
|
|
2018-10-15 20:07:23 +00:00
|
|
|
col_layout = [[sg.Table(values=data,
|
|
|
|
headings=header_list,
|
|
|
|
max_col_width=25,
|
|
|
|
auto_size_columns=True,
|
|
|
|
justification='right',
|
|
|
|
alternating_row_color='lightblue',
|
|
|
|
num_rows=len(data))]]
|
2018-09-20 01:11:45 +00:00
|
|
|
|
|
|
|
canvas_size = (13*10*len(header_list), 600) # estimate canvas size - 13 pixels per char * 10 char per column * num columns
|
|
|
|
layout = [[sg.Column(col_layout, size=canvas_size, scrollable=True)],]
|
|
|
|
|
2018-09-24 22:01:00 +00:00
|
|
|
window = sg.Window('Table', grab_anywhere=False).Layout(layout)
|
2018-10-15 20:07:23 +00:00
|
|
|
event, values = window.Read()
|
2018-09-20 01:11:45 +00:00
|
|
|
|
2018-09-24 22:01:00 +00:00
|
|
|
sys.exit(69)
|
2018-09-20 01:11:45 +00:00
|
|
|
|
|
|
|
table_example()
|