0

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.

  • ***the following function is called:***: As it stands, this will throw a Error. A `command=` does not pass any argument! – stovfl Apr 08 '20 at 14:48
  • @stovfl It does not throw an error for me though. – Emrah Tortumlu Apr 08 '20 at 14:53
  • So, `def autocontroll(...`, is not called? – stovfl Apr 08 '20 at 15:06
  • @stovfl It is called in the line: newMenu = OptionMenu(canvasframe, customvar, *customalts, command=autocontroll) I agree that it should ask for a parameter and give an error, but it doesnt seem to do that. – Emrah Tortumlu Apr 08 '20 at 15:25
  • ***It is called in the line: newMenu =***: No, there it is definend not called. Verify if the Funktion get called, may i see the output of `print(action)` – stovfl Apr 08 '20 at 15:40

0 Answers0