-1

So my friend challenged me to make something with Python. I haven't really done Python before, so I'm struggling with quite a few things.

I have a 9 buttons

turn7 = Button(root, text='', relief="flat", font=("arial", 28, "bold"), width=3, command=lambda: doTurn(turn7,7))

I create every one of the buttons like this and then append them in a list:

_list.append([turn1,turn2,turn3,turn4,turn5,turn6,turn7,turn8,turn9])

I'm trying to change the text of all the buttons like this:

for turn in _list:
    turn["text"] = "Text"

But it returns: "TypeError: list indices must be integers or slices, not str"

I have no idea what to do.

Thanks in advance.

  • Possible duplicate https://stackoverflow.com/questions/32615440/python-3-tkinter-how-to-update-button-text - does this help? – Thomas May 06 '20 at 12:23
  • 2
    Try changing `_list.append(...)` to `_list.extend(...)`. – acw1668 May 06 '20 at 12:25
  • Thanks, list.extend() fixed it. And the TextVariable method is actually pretty handy too. – metamethod May 06 '20 at 12:29
  • Does this answer your question? [Python 3, Tkinter, How to update button text](https://stackoverflow.com/questions/32615440/python-3-tkinter-how-to-update-button-text) – stovfl May 06 '20 at 13:47
  • When you write ```turn["text"]```, you can't have a string. You need to have a number, so you need to do ```_list[turn]``` instead. – 10 Rep May 06 '20 at 17:39

1 Answers1

0

The problem is that your list looks like [[turn1,turn2,turn3,turn4,turn5,turn6,turn7,turn8,turn9]], not [turn1,turn2,turn3,turn4,turn5,turn6,turn7,turn8,turn9]. Use _list += [turn1,turn2,turn3,turn4,turn5,turn6,turn7,turn8,turn9] instead. Hope that's helpful!

rizerphe
  • 1,340
  • 1
  • 14
  • 24