4

I'm new to PySimpleGUI and pandas. I want to make a table in the GUI. How can I add the entries of each header? My code has problems.

import pandas
import PySimpleGUI
headers = {'Integers':[], 'Strings':[], 'Normallized Floats':[], 'Binaries':[]}
table = pandas.DataFrame(headers)
window = PySimpleGUI.Window(title = 'Sample excel file', layout = [[PySimpleGUI.Table(values = table, headings = list(headers))]] , margins = (200,200))
event, value = window.read()
Emad
  • 155
  • 1
  • 7

1 Answers1

4

Data for table is row list of column list.

Here, no data record, to avoid error, have to set option auto_size_columns=False and width for each column for sg.Table.

import pandas
import PySimpleGUI as sg

headers = {'Integers':[], 'Strings':[], 'Normalized Floats':[],
    'Binaries':[]}
table = pandas.DataFrame(headers)

headings = list(headers)
values = table.values.tolist()

sg.theme("DarkBlue3")
sg.set_options(font=("Courier New", 16))

layout = [[sg.Table(values = values, headings = headings,
    # Set column widths for empty record of table
    auto_size_columns=False,
    col_widths=list(map(lambda x:len(x)+1, headings)))]]

window = sg.Window('Sample excel file',  layout)
event, value = window.read()
Jason Yang
  • 11,284
  • 2
  • 9
  • 23
  • Thanks for answering. I just don't understand one thing: col_widths. Will you explain how you assigned that value to it? – Emad Mar 05 '21 at 06:25
  • `col_widths` set the widths for each column. Here I measured length of each headings by lambda function to get extra 1 length. If extra 1 length is not required, just `len` and no lambda. `map` function will applied this function to each element of list. – Jason Yang Mar 05 '21 at 06:32
  • We can say headings is the domain of that function and x is the variable and len(x + 1) is the rule for mapping and list() function is like getting the range of that function as a list. – Emad Mar 05 '21 at 06:57