简体   繁体   中英

Why Doesn't Tkinter Menu Update

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM