I am pretty new to Python as well as coding in general.
I've got 42 Labels (Strings) stored in a list. My aim is to: for each of the 42 labels, create a Tkinter Label displaying the String and a Dropdown List (Tkinter Optionmenu).
Two problems occur:
varDd[i] = StringVar(root) IndexError: list assignment index out of range
My complete Code: https://codepaste.net/6jkuza
The essential part:
def createDdList():
del labelsDd[:]
del listsDd[:]
if len(labels) > 1:
varDd = [len(labels)]
for i,label in enumerate(labels):
# Erstelle Labels mit den Markerlabels im scrollbaren Canvas
labelsDd.append(Label(contentCanvas,text=label))
labelsDd[i].pack()
# Erstelle die Dropdown Listen im scrollbaren Canvas
varDd[i] = StringVar(root)
listsDd.append(OptionMenu(canvas,varDd[i],*labels))
listsDd[i].place(x=70,y=10+(30*i))
contentCanvas = Frame(canvas,bg='#FFFFFF')
canvas.create_window((0,375),window=contentCanvas)
The first issue arrises from this like:
varDd = [len(labels)]
While this syntax is used in other languages to mean "a list with this many elements in python it is just a list with one number:
>>> labels = range(4)
>>> v = [len(labels)]
>>> v
[4]
Instead you probably want something along the lines of this:
>>> varDb = [None]*len(labels)
>>> varDb
[None, None, None, None]
Then you can index it the same way as your other lists
The other issue is that using .place()
inside a canvas places it on the screen but not on the canvas space itself, to do that you need to call canvas.create_window
so this line:
listsDd[i].place(x=70,y=10+(30*i))
would translate to this:
canvas.create_window(70, 10+(30*i), window = listsDd[i])