Fix for autosized columns. NEW Table Demos - CSV & Panda
This commit is contained in:
parent
f850785532
commit
5fbfed05f7
|
@ -0,0 +1,46 @@
|
||||||
|
import csv
|
||||||
|
import PySimpleGUI as sg
|
||||||
|
|
||||||
|
def table_example():
|
||||||
|
filename = sg.PopupGetFile('filename to open', no_window=True, file_types=(("CSV Files","*.csv"),))
|
||||||
|
# --- populate table with file contents --- #
|
||||||
|
if filename == '':
|
||||||
|
exit(69)
|
||||||
|
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')
|
||||||
|
exit(69)
|
||||||
|
sg.SetOptions(element_padding=(0, 0))
|
||||||
|
|
||||||
|
col_layout = [[sg.Table(values=data, headings=header_list, max_col_width=25,
|
||||||
|
auto_size_columns=True, justification='right', size=(None, len(data)))]]
|
||||||
|
|
||||||
|
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)],]
|
||||||
|
|
||||||
|
form = sg.FlexForm('Table', grab_anywhere=False)
|
||||||
|
b, v = form.LayoutAndRead(layout)
|
||||||
|
|
||||||
|
exit(69)
|
||||||
|
|
||||||
|
table_example()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,40 +1,38 @@
|
||||||
import pandas as pd
|
import pandas as pd
|
||||||
import PySimpleGUI as sg
|
import PySimpleGUI as sg
|
||||||
|
|
||||||
"""
|
|
||||||
|
|
||||||
Display a CSV file using Table Element and Pandas
|
|
||||||
Thank you to for writing this demo
|
|
||||||
|
|
||||||
"""
|
|
||||||
|
|
||||||
|
|
||||||
def table_example():
|
def table_example():
|
||||||
|
sg.SetOptions(auto_size_buttons=True)
|
||||||
filename = sg.PopupGetFile('filename to open', no_window=True, file_types=(("CSV Files", "*.csv"),))
|
filename = sg.PopupGetFile('filename to open', no_window=True, file_types=(("CSV Files", "*.csv"),))
|
||||||
# --- populate table with file contents --- #
|
# --- populate table with file contents --- #
|
||||||
|
if filename == '':
|
||||||
|
exit(69)
|
||||||
data = []
|
data = []
|
||||||
header_list = []
|
header_list = []
|
||||||
|
button = sg.PopupYesNo('Does this file have column names already?')
|
||||||
if filename is not None:
|
if filename is not None:
|
||||||
try:
|
try:
|
||||||
df = pd.read_csv(filename, sep=',', engine='python') # read everything else into a list of rows
|
df = pd.read_csv(filename, sep=',', engine='python', header=None) # Header=None means you directly pass the columns names to the dataframe
|
||||||
header_list = df.columns.tolist()
|
data = df.values.tolist() # read everything else into a list of rows
|
||||||
data = df.values.tolist()
|
if button == 'Yes': # Press if you named your columns in the csv
|
||||||
# print(data)
|
header_list = df.iloc[0].tolist() # Uses the first row (which should be column names) as columns names
|
||||||
|
data = df[1:].values.tolist() # Drops the first row in the table (otherwise the header names and the first row will be the same)
|
||||||
|
elif button == 'No': # Press if you didn't name the columns in the csv
|
||||||
|
header_list = ['column' + str(x) for x in range(len(data[0]))] # Creates columns names for each column ('column0', 'column1', etc)
|
||||||
except:
|
except:
|
||||||
sg.PopupError('Error reading file')
|
sg.PopupError('Error reading file')
|
||||||
exit(69)
|
exit(69)
|
||||||
|
# sg.SetOptions(element_padding=(0, 0))
|
||||||
|
|
||||||
sg.SetOptions(element_padding=(0, 0))
|
col_layout = [[sg.Table(values=data, headings=header_list, display_row_numbers=True,
|
||||||
|
auto_size_columns=False, size=(None, len(data)))]]
|
||||||
|
|
||||||
col_layout = [[sg.Table(values=data, headings=header_list, max_col_width=20,
|
canvas_size = (13*10*len(header_list), 600) # estimate canvas size - 13 pixels per char * 10 per column * num columns
|
||||||
auto_size_columns=True, justification='right', size=(None, len(data)))]]
|
layout = [[sg.Column(col_layout, size=canvas_size, scrollable=True)]]
|
||||||
|
|
||||||
layout = [[sg.Column(col_layout, size=(1200, 600), scrollable=True)]]
|
|
||||||
|
|
||||||
form = sg.FlexForm('Table', grab_anywhere=False)
|
form = sg.FlexForm('Table', grab_anywhere=False)
|
||||||
b, v = form.LayoutAndRead(layout)
|
b, v = form.LayoutAndRead(layout)
|
||||||
|
|
||||||
exit(69)
|
exit(69)
|
||||||
|
|
||||||
|
|
||||||
table_example()
|
table_example()
|
||||||
|
|
|
@ -2180,6 +2180,7 @@ def ReadFormButton(button_text, image_filename=None, image_size=(None, None),ima
|
||||||
|
|
||||||
ReadButton = ReadFormButton
|
ReadButton = ReadFormButton
|
||||||
|
|
||||||
|
|
||||||
# ------------------------- Realtime BUTTON Element lazy function ------------------------- #
|
# ------------------------- Realtime BUTTON Element lazy function ------------------------- #
|
||||||
def RealtimeButton(button_text, image_filename=None, image_size=(None, None),image_subsample=None,border_width=None,scale=(None, None), size=(None, None), auto_size_button=None, button_color=None, font=None, bind_return_key=False, focus=False, pad=None, key=None):
|
def RealtimeButton(button_text, image_filename=None, image_size=(None, None),image_subsample=None,border_width=None,scale=(None, None), size=(None, None), auto_size_button=None, button_color=None, font=None, bind_return_key=False, focus=False, pad=None, key=None):
|
||||||
return Button(BUTTON_TYPE_REALTIME, image_filename=image_filename, image_size=image_size, image_subsample=image_subsample, border_width=border_width, button_text=button_text, scale=scale, size=size, auto_size_button=auto_size_button, button_color=button_color, font=font, bind_return_key=bind_return_key, focus=focus, pad=pad, key=key)
|
return Button(BUTTON_TYPE_REALTIME, image_filename=image_filename, image_size=image_size, image_subsample=image_subsample, border_width=border_width, button_text=button_text, scale=scale, size=size, auto_size_button=auto_size_button, button_color=button_color, font=font, bind_return_key=bind_return_key, focus=focus, pad=pad, key=key)
|
||||||
|
@ -2956,7 +2957,7 @@ def PackFormIntoFrame(form, containing_frame, toplevel_form):
|
||||||
for i, heading in enumerate(element.ColumnHeadings):
|
for i, heading in enumerate(element.ColumnHeadings):
|
||||||
treeview.heading(heading, text=heading)
|
treeview.heading(heading, text=heading)
|
||||||
if element.AutoSizeColumns:
|
if element.AutoSizeColumns:
|
||||||
width = column_widths[i]
|
width = max(column_widths[i], len(heading))
|
||||||
else:
|
else:
|
||||||
try:
|
try:
|
||||||
width = element.ColumnWidths[i]
|
width = element.ColumnWidths[i]
|
||||||
|
|
Loading…
Reference in New Issue