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:
- I get an index error for the list of variables (for the optionmenus), here is the output of my Console:
varDd[i] = StringVar(root) IndexError: list assignment index out of range
- The Canvas is Scrollable as I intended, but the content doesn't scroll with it
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:
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:
Instead you probably want something along the lines of this:
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 callcanvas.create_window
so this line:would translate to this: