Home > Software design >  SytanxError with sg. argument from PySimpleGUI in Python
SytanxError with sg. argument from PySimpleGUI in Python

Time:11-06

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?

CodePudding user response:

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)

CodePudding user response:

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]
  • Related