0

I am trying to make a tkinter menu's options change when a function is entered. I can verify that the function is entered, but for some reason the function's contents do not seem to be updating the tkinter menu's options. Here is a snippet of the relevant code:

def func(selection):
    global menuOptions;
    menuOptions = men[selection];
    root.update();

men = {
  "x1": ["a"],
  "x2": ["b", "c"],
  "x3": ["d", "e", "f"],
  "x4":["g", "h", "i", "j"]
};

menuOptions = [""];

y = tkinter.StringVar();
y.set("");
tkinter.OptionMenu(root, y, *menuOptions).place(x=5, y=5);

As I mentioned, the function is definitely being entered by the rest of the code, but the menu options are not being updated. Any help is appreciated. I am using the most recent version of python and tkinter.

Thanks

stovfl
  • 14,998
  • 7
  • 24
  • 51
ALec
  • 141
  • 3
  • 11
  • Does this answer your question? [Updating OptionMenu from List](https://stackoverflow.com/questions/28412496/updating-optionmenu-from-list) – stovfl Jan 30 '20 at 21:08

1 Answers1

-1

You can use this code:

import tkinter
def func(selection):
    global menuOptions;
    menuOptions = men[selection];
    root.update();
    return menuOptions

men = {
  "x1": ["a"],
  "x2": ["b", "c"],
  "x3": ["d", "e", "f"],
  "x4":["g", "h", "i", "j"]
};

menuOptions = [""];
y = tkinter.StringVar();
y.set("");
tkinter.OptionMenu(root, y, *menuOptions).place(x=5, y=5)
tkinter.OptionMenu(root, y, func('x1')).place(x=5, y=5) # i am changed the menu list
tkinter.Button(root, text='change menu', command=lambda: func('x1')).place(x=30, y=30) # i am bind changes to Button
n1tr0xs
  • 408
  • 2
  • 9
  • 1
    According to this answer, [options in an OptionMenu are not bound to the list from which they are created](https://stackoverflow.com/a/28412967/7414759), and therefore your answer will not work. Furthermore, you can't return anything from a `tkinter callback`. – stovfl Jan 30 '20 at 21:06