listelementpysimpleguiskip

PySimpleGUI Table index out of error when I skip first element of list


I'm making a small program with PySimpleGUI and encountered a very weird problem that doesn't make any sense to me.

def open_view_window(date):

    calendar_dates = db_session.factory().query(Calendar.full_date).all()
    calendar_dates = [ e[0] for e in calendar_dates ]
    calendar_dates.sort(key=lambda date: datetime.strptime(date, "%d-%m-%Y"))
    calendar_dates.insert(0, "")
    print(calendar_dates)
    tag_names = db_session.factory().query(Tag.name).all()
    tag_names = [ e[0] for e in tag_names ]
    tag_names.insert(0, "")
    print(tag_names)

    if date in calendar_dates:
        default_val = date
    else:
        default_val = calendar_dates[1]

    view_layout = [[sg.Text('Filter by:', key='FILTER')],
                   [sg.Text('Date:'), sg.Combo(values=calendar_dates, default_value=default_val, readonly=True), sg.Text('Tag:'), sg.Combo(values=tag_names, default_value=tag_names[0], readonly=True)],
                   [sg.Table(values=calendar_dates[1:])]]

As you can see in the code above, I have a list calendar_dates which I create from the contents of my database. After I sort the list, I insert a new empty element at the first position of the list. But when I try to pass the list as an argument to the PySimpleGUI Table element and skip the first element with [1:], as it's no use for my case, I get the following error:

_tkinter.TclError: Column index 1 out of bounds

As I was trying to find out what I was doing wrong, I changed the list to skip the last element instead of the first with [:1], and that worked for some reason.

Please help, what am I doing wrong?


Solution

  • Set the headings for your Table element, or you will get exception

    _tkinter.TclError: Column index ? out of bounds
    

    Try to remove code not related to your question, reduced code for your question here.

    import PySimpleGUI as sg
    
    data = [['06/10/23'], ['06/11/23'], ['06/12/23'], ['06/13/23']]
    calendar_dates = [date[0] for date in data]
    data.insert(0, [""])
    
    sg.set_options(font=("Courier New", 12))
    layout = [
        [sg.Text('Filter by:', key='FILTER')],
        [sg.Text('Date:'), sg.Combo(values=calendar_dates)],
        [sg.Table(values=data[1:], headings=["Date"], auto_size_columns=False, col_widths=[10], expand_x=True, justification='center')],
    ]
    sg.Window('Demo', layout).read(close=True)