Search code examples
pythontypeerrorpysimplegui

I get "TypeError: list indices must be integers or slices, not Button" while running my GUI app


I am working on a project built by PySimpleGUI in Python and I'd created config.ini file in advance for settings window so that user could interact with program more flexibly. Basically, settings refers to this file. There were some following errors but the first occurence is in the below mentioned line.

def settings_window(settings):

        layout = [[sg.Text("SETTINGS")],
                  [sg.Text("Separator"), sg.Input(settings["CSV"]["separator"], s=1, key="-SEPARATOR-"),
                   sg.Text("Decimal"), sg.Combo(settings["CSV"]["decimal"].split("|"),
                                                default_value = settings["CSV"]["decimal_default"],
                                                s=1, key="-DECIMAL-"),
                    
                    sg.Text("Sheet Name:"), sg.Input(settings["Excel"]["sheet_name"], s=20, key="-SHEET_NAME-")]

                  [sg.Button("Save Current Settings", s=20)]]
        
        window = sg.Window(title="Settings", layout = layout, modal=True)

        while True:
            event, values = window.read()
            if event == sg.WIN_CLOSED:
                break
            if event == "Save Current Settings":
                settings["CSV"]["separator"] == values["-SEPARATOR"]
                settings["CSV"]["decimal"] == values["-DECIMAL-"]
                settings["Excel"]["sheet_name"] == values["-SHEET_NAME-"]

                sg.popup_no_titlebar("Settings saved!")
                break
        
        window.close()

if __name__ == "__main__":
    SETTINGS_PATH = Path.cwd()
    
    settings = sg.UserSettings(
        path = SETTINGS_PATH, filename="config.ini",
        use_config_file=True,
        convert_bools_and_none=True
    )
    
    theme = settings["GUI"]["theme"]
    font_family = settings["GUI"]["font_family"]
    font_size = int(settings["GUI"]["font_size"])

    sg.theme(theme)
    sg.set_options(font=(font_family, font_size))
    main_window()

The first error I came across is File "c:\Users\User\Desktop\Creating_YourOwn_Package\main.py", line 12, in settings_window [sg.Text("Separator"), sg.Input(settings["CSV"]["separator"], s=1, key="-SEPARATOR-"), TypeError: list indices must be integers or slices, not Button


Solution

  • You forgot to add a "," add the end of your second list.

     [[sg.Text("SETTINGS")],
                      [sg.Text("Separator"), sg.Input(settings["CSV"]["separator"], s=1, key="-SEPARATOR-"),
                       sg.Text("Decimal"), sg.Combo(settings["CSV"]["decimal"].split("|"),
                                                    default_value = settings["CSV"]["decimal_default"],
                                                    s=1, key="-DECIMAL-"),
                        
                        sg.Text("Sheet Name:"), sg.Input(settings["Excel"]["sheet_name"], s=20, key="-SHEET_NAME-")], # YOU FORGOT COMA HERE
    
                      [sg.Button("Save Current Settings", s=20)]]
    

    So instead of adding a list, python thinks you are trying to access a specific index of the second list. That is why it's throw a TypeError. Now it should be fixed