So I am writing a GUI where you start with 1 optionmenu and the user can add more optionmenus. To add the additional optionmenus, I use a function:
def addmove():
global movecount, customalts, customvarlist
if ((customvarlist[-1] == "Choose action")) == False:
customvar = StringVar()
movecount += 1
#print(customvarlist[index(movecount)])
newMenu = OptionMenu(canvasframe, customvar, *customalts, command=autocontroll)
newMenu.grid(row=movecount,column=0)
#customvarlist.insert(movecount, customvar)
customvarlist.append(customvar.get())
Each time an option is selected, the following function is called:
def autocontroll(action):
global customvarlist
print(action)
if len(customvarlist) == 0:
customvarlist.append(action)
else:
if(customvarlist[-1] == "Choose action") and ((action != "Choose action")):
customvarlist = customvarlist[:-1]
customvarlist.append(action)
print(customvarlist)
I want to be able to create a list of the selected options, which I do by getting the variable from the optionmenu. But the problem arises when the user decides to choose one item and then change to another. E.g. if the options are [item1, item2, item3], and the user chooses 2 items, [item1, item2], but then changes to [item3, item2], my list does not update, it remains at the first order chosen.
One idea I had was to try to find which optionmenu is executing the call, in order to know in which order in the list that I need to update the item. But I don't know any way of doing this.