Search code examples
pythonpython-3.xsyntax-error

Why my for loop isn't working right for the menus in tkinter python


out putI just want to make sure that the label and command should work properly

list = {'About', 
'Experience'}
comand = ['about','experience']
for i in range(len(list)):
    for t in range(len(comand)):
        help_menu.add_command(label=str(i), command=str(t))

I also have tried this

list = {'About', 
'Experience'}
comand = ['about','experience']
for i in range(len(list)):
    for t in range(len(comand)):
        help_menu.add_command(label='list'+str(i), command='comand'+str(t))

Solution

  • The reason it isn't working is because you are using a nested for loop. By nesting the for loops you are essentially looping through that many times more than you want to. Try just using a single for loop that assigns the value from both lists using the single index.

    I think you are trying to do this:

    lst = ['About', 'Experience']
    comand = [about,experience]
    for i in range(len(list)):
        help_menu.add_command(label=lst[i], command=comand[i])