0

The OptionMenu in tkinter takes any number of parameters for the options in the menu such as follows:

import tkinter as tk
newMenu = tk.OptionMenu(self, clicked, "A", "B", command=doSomething)

Is there a way to update the number of argumenets in OptionMenu so I can go from the above code to this new code:

newMenu = tk.OptionMenu(self, clicked, "A", "B", "C", "D", command=doSomething)
jizhihaoSAMA
  • 12,336
  • 9
  • 27
  • 49
  • Does this answer your question? [updating-optionmenu-from-list](https://stackoverflow.com/questions/28412496) – stovfl Jun 17 '20 at 09:05

1 Answers1

3

tk.OptionMenu has a Menu widget in it.

If you want to add some values,you could use add_command:

for i in ["C","D"]:
    newMenu['menu'].add_command(label=i)

If you want to remove some values, use delete:

newMenu['menu'].delete("0",tk.END) # this will remove all the values

@acw1668 pointed out a big problem in my code,if you want to also bind command and change the clicked variable.

Recommend this(acw1668 sugguests):

for i in ["C", "D"]:
    newMenu['menu'].add_command(label=i, command=tk._setit(clicked, i, doSomething))

this also could do but don't recommend:

for i in ["C", "D"]:
    newMenu['menu'].add_command(label=i, command=lambda i=i:clicked.set(i) or doSomething(i))
jizhihaoSAMA
  • 12,336
  • 9
  • 27
  • 49