2

When I try to place variables as text, it says this: TypeError: 'Text' object is not subscriptable

Here is the code I used to generate this error:

import PySimpleGUI as sg


font1 = ("Calibri", 15)

text1 = "PlaceholderText1"
text2 = "PlaceholderText2"
text3 = "PlaceholderText3"
text4 = "PlaceholderText4"
text5 = "PlaceholderText5"
text6 = "PlaceholderText6"
text7 = "PlaceholderText7"
text8 = "PlaceholderText8"
text9 = "PlaceholderText9"
text10 = "PlaceholderText10"

def stock():
    
    layout = [[sg.Text("Our Stock:", font=font1)
    [sg.Text(text1)]
    [sg.Text(text2)]
    [sg.Text(text3)]
    [sg.Text(text4)]
    [sg.Text(text5)]
    [sg.Text(text5)]
    [sg.Text(text6)]
    [sg.Text(text7)]
    [sg.Text(text8)]
    [sg.Text(text9)]
    [sg.Text(text10)]]]
    window = sg.Window("Our Stock", layout, modal=True, size=(250, 300))
    choice = None
    while True:
        event, values = window.read()
        if event == "Exit" or event == sg.WIN_CLOSED:
            break
        
    window.close()


def lobbyWindow():
    layout = [[sg.Text("What do you want to buy?")],
                [sg.Input(key='INPUT1')],
                [sg.Button("View Stock"), sg.Button("Exit"), sg.Button("Admin Panel")]]

    window = sg.Window("The Store", layout)

    while True:
        event, values = window.read()

        if event == "Exit" or event == sg.WIN_CLOSED:
            break
        if event == "View Stock":
            stock()
        
    window.close()
lobbyWindow()

It should carry on with the placeholder text because I set it outside any functions. I think there's something to do this, but I don't know how to do it.

Dev
  • 21
  • 2
  • Check the format of your `layout`, is it in format `list of list` ? comma required between items in a list. – Jason Yang Oct 25 '22 at 16:46

1 Answers1

0

Sorry if I misunderstand your question, but here is some information:

First, your layout = line is not valid Python code. You probably want to make a list, which requires commas between them. Without the commas, it looks like you are indexing some deep list of lists of list of lists. So more like,

layout = [[sg.Text("Our Stock:", font=font1),
          [sg.Text(text1)],
          ...

Second, everyone starts by getting caught on the one pair of braces per line. You want the first line to close the first brace, making it the first line, so:

    layout = [[sg.Text("Our Stock:", font=font1)],

Finally, once this example works, you have some other options. You can use key= options on a field to update later. So [sg.Text(text1, key='key1')] would let you do window['key1'].update("New Text"). You might also look at the multiline to just use a string with new lines.

I hope this answers your question.

Keep hacking! Keep notes.

Charles

Charles Merriam
  • 19,908
  • 6
  • 73
  • 83