se7en94
se7en94

Reputation: 45

PySimpleGUI tables - unable to create list of lists

I'm unable to use PySimpleGUI.Table because of my inability to create list of lists, please see my code:

def is_compiled(file):
    full_path = main_path + 'models_data/'+ file[:-3]
    return isdir(full_path)   #Returns bool True of False


models_src = [f for f in listdir(model_src_path) if isfile(join(model_src_path, f))] 
is_compiled_list = [str(is_compiled(f)) for f in models_src] 
table_list = [models_src,is_compiled_list]
print(table_list)

printing my lists shows:

[['CNN_v1.py', 'test.py'], ['False', 'True']]

and type is <class 'list'>

But when I try to put this list into sg.Table:

table_headings = ['name','iscompiled?']
layout = [[sg.Table(table_list,headings=table_headings]]
window = sg.Window("Demo", layout, margins=(500, 300))
while True:
    event, values = window.read()

I'm getting this error message:

list indices must be integers or slices, not Text

I know this is probably a very simple solution but I was trying to find it for hours and I couldn't. Thanks for help!

EDIT:Typo

Upvotes: 1

Views: 341

Answers (1)

Jason Yang
Jason Yang

Reputation: 13061

This exception maybe caused by the code you not show here.

For example, in following code, [sg.Table(table_list, headings=table_headings)] come with [sg.Text('Hellow World')] at next line.

import PySimpleGUI as sg

table_list = [['CNN_v1.py', 'test.py'], ['False', 'True']]
table_headings = ['name','iscompiled?']
layout = [
    [sg.Table(table_list, headings=table_headings)]
    [sg.Text('Hellow World')]
]
sg.Window('Title', layout).read(close=True)

It is something like following code, sg.Text('Hellow World') will be thought as the index of list1, that's why you got exception TypeError: list indices must be integers or slices, not Text

list1 = [sg.Table(table_list, headings=table_headings)]
layout = [
    list1[sg.Text('Hellow World')]
]

It should be with a comma between any two items in a list, like

import PySimpleGUI as sg

table_list = [['CNN_v1.py', 'test.py'], ['False', 'True']]
table_headings = ['name','iscompiled?']
layout = [
    [sg.Table(table_list, headings=table_headings)],    # A comma missed between any two items
    [sg.Text('Hellow World')]
]
sg.Window('Title', layout).read(close=True)

Upvotes: 2

Related Questions