list - Python function not working - Tkinter -
my function isn't giving me right output, , doesn't want work. keep getting error:
typeerror: list indices must integers, not str
this 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 = [] 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 display in shop list based on button is...
remove temp = shop[i]
code
for in shop: temp = tkinter.button(top, height=10, width=100, text = temp, command = shopping(i)) temp.pack() buttons.append(temp)
the for
loop iterates on elements in list , not indices!. python docs make more clear
the statement in python differs bit may used in c or pascal. rather iterating on arithmetic progression of numbers (like in pascal), or giving user ability define both iteration step , halting condition (as c), python’s statement iterates on items of sequence (a list or string), in order appear in sequence.
also note command
argument in button constructor takes function argument. maybe better off writing command = shopping
there instead of call command = shopping(i)
.
Comments
Post a Comment