2

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

se7en94
  • 55
  • 4

1 Answers1

2

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)
Jason Yang
  • 11,284
  • 2
  • 9
  • 23
  • Oh my god, you are a genius! List was all right all the time, all I was missing was a comma between Table entry and next line in layout. – se7en94 Feb 03 '22 at 12:15
  • 1
    I'll add this specific error message to the documentation. It's an error I make frequently as well.... pesky commas. It's not an error I can "catch" in the PySimpleGUI code itself because it's localized to your program. The best I think I can do is document the exact error message in case someone does a search. – Mike from PSG Feb 04 '22 at 11:47