我正在尝试在输入函数时更改tkinter菜单的选项。我可以验证函数是否已进入,但由于某些原因,函数的内容似乎没有更新tkinter菜单的选项。以下是相关代码的一个片段:
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);正如我所提到的,函数肯定是由其余的代码进入的,但是菜单选项并没有更新。任何帮助都是非常感谢的。我使用的是最新版本的python和tkinter。
谢谢
发布于 2020-01-31 05:01:56
您可以使用以下代码:
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 Buttonhttps://stackoverflow.com/questions/59994347
复制相似问题