0

My function isn't giving me the right output, and it doesn't want to work. I keep getting this error:

TypeError: list indices must be integers, not str

This is my code:

    def showShop(level = level, cash = cash):
      top = Tkinter.Tk()
      shop = ["$100 & level 2 - Shotgun", "$250 & level 3 - 5 Grenades", "$500 & level 5 - Rocket Launcher"]
      buttons = []
      for i in shop:
        temp = shop[i]
        temp = Tkinter.Button(top, height=10, width=100, text = temp, command = shopping(i))
        temp.pack()
        buttons.append(temp)
      top.mainloop()

I want it to display what is in the shop list based on what button it is...

2
  • I might not be that smart.... Commented Apr 29, 2015 at 13:15
  • Please accept an answer if you think it solves your problem. It will community at large to recognize the correct solution. This can be done by clicking the green check mark next to the answer. See this image for reference. Cheers. Commented Nov 28, 2015 at 14:52

3 Answers 3

6

Remove temp = shop[i] from the code

for i in shop:
        temp = Tkinter.Button(top, height=10, width=100, text = temp, command = shopping(i))
        temp.pack()
        buttons.append(temp)

The for loop iterates over the elements in the list and not the indices!. The python docs make it more clear

The for statement in Python differs a bit from what you may be used to in C or Pascal. Rather than always iterating over an arithmetic progression of numbers (like in Pascal), or giving the user the ability to define both the iteration step and halting condition (as C), Python’s for statement iterates over the items of any sequence (a list or a string), in the order that they appear in the sequence.

Also note that the command argument in the Button constructor takes a function as an argument. So you maybe better off by writing command = shopping there instead of the call command = shopping(i).

Sign up to request clarification or add additional context in comments.

2 Comments

Might also be of use to point out the inevitable issues caused by command = shopping(i)
@Jkdc We do not know what the function shopping does! Yet it is better to add a line on that! Thanks :)
0

Change for i in shop to for i in xrange(shop).

1 Comment

When I run the think though, it says that i in settings(i) is always equal to four....
0

You have to use something like partial to pass arguments to the function called by the button press. Note that you have declared the variable "temp" as 2 different things. The only reason it works is because the second declaration is after you use the first. Also note that the "buttons" list can not be used outside of the function showShop() because it is created in/local to that function. The following is working code based on what you posted. Also, please do not use "i", "l" or "O" as single digit variable names as they can look like numbers.

import Tkinter
from functools import partial

def shopping(btn_num):
   print "button number %d pressed" % (btn_num)
   buttons[btn_num]["bg"]="lightblue"

def showShop(buttons):
      top = Tkinter.Tk()
      shop = ["$100 & level 2 - Shotgun", "$250 & level 3 - 5 Grenades",
              "$500 & level 5 - Rocket Launcher"]

      ##buttons = []
      for ctr in range(len(shop)):
        temp = Tkinter.Button(top, height=10, width=100, text = shop[ctr],
               command = partial(shopping, ctr))
        temp.pack()
        buttons.append(temp)
      top.mainloop()

## lists are mutable
buttons=[]  ## not local to the function
showShop(buttons)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.