Home > Blockchain >  Tkinter OptionMenu command not entering
Tkinter OptionMenu command not entering

Time:04-22

I'm creating an OptionMenu in tkinter and assigning it a function to run whenever I choose an option:

dropdown = tk.OptionMenu(root, var, *dropdownStr, command=functiona)
dropdown.pack()

I also have another function that completely clears out dropdown and re-fills it, since the other function will update or completely change the contents of dropdownStr.

var.set('')
dropdown['menu'].delete(0, 'end')
for string in dropdownStr:
    dropdown['menu'].add_command(label=string, command=tk._setit(var, string))

This works as intended, and the dropdown is updated the way I want. However, from this point on, the dropdown menu no longer calls into functiona. There's no error or complaint, but it's as if I never set the command when creating the menu. I have tried dropdown.configure to re-set the command to be functiona, but that doesn't seem to be working. How can I avoid wiping the call into functiona out, or at the very least, re-set it after I clear the dropdown?

CodePudding user response:

You need to pass functiona as the 3rd argument of tk._setit():

var.set('')
dropdown['menu'].delete(0, 'end')
for string in dropdownStr:
    dropdown['menu'].add_command(label=string, command=tk._setit(var, string, functiona))
  • Related