SytanxError with sg. argument from PySimpleGUI in Python

Question:

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?
Asked By: Oliveira

||

Answers:

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)
Answered By: sj95126

If you need to access an object in a list, you either need to create it before hand, like this:

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

Or do something ugly that relies on knowing the internal structure, like this:

layout = [
    [sg.Text("Hello from PySimpleGUI")], 
    [sg.Button("Close")],
    [sg.Window("Demo")]
]
window = layout[2][0]
Answered By: Tim Roberts
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.