Search code examples
pythonsyntax-errorpysimplegui

SytanxError with sg. argument from PySimpleGUI in Python


I want to make a widget window, but the Bottom and Text part aren't working properly and it keps getting sintax errors at the same part:

import PySimpleGUI as sg

layout = [
    [sg.Text("Hello from PySimpleGUI")], 
    [sg.Button("Close")]
    window = sg.Window("Demo, layout")
]
while true:
    event, values = window.read()
    if event == "Close" or event == sg.WIN_CLOSED:
        break

window.close()

it says I forgot a comma, but the references I checked for this code didn't use one, and I tried changing the elements or just putting the comma, but it didn't work either.

ERROR message:

line 5
    [sg.Buttom("OK")]
    ^^^^^^^^^^^^^^^^^
SyntaxError: invalid syntax. Perhaps you forgot a comma?

Solution

  • This:

    layout = [
        [sg.Text("Hello from PySimpleGUI")], 
        [sg.Button("Close")]
        window = sg.Window("Demo, layout")
    ]
    

    isn't a valid list, because you can't include an assignment like that, and there should be a comma after the second element. Also, the call to sg.Window() doesn't look right, because presumably you meant to pass layout as the second argument to define the layout of a window named "Demo".

    Try doing this:

    layout = [
        [sg.Text("Hello from PySimpleGUI")], 
        [sg.Button("Close")]
    ]
    window = sg.Window("Demo", layout)