更改tkinter中传递的小部件的菜单选项

时间:2018-03-01 12:19:56

标签: python tkinter

如果我必须通过给出v变量的原始引用

来更改选项的值,这将正常工作
import tkinter as tk

root = tk.Tk()
optionList = ('a', 'b', 'c')
v = tk.StringVar()
v.set(optionList[0])  # Here is the initially selected value
om = tk.OptionMenu(root, v, *optionList)
om.pack()

v.set(optionList[2]) # This one will be the final selected value 
root.mainloop()

但问题是当我们尝试通过widget.winfo_children()获取元素并将其值设置如下:

import tkinter as tk

root = tk.Tk()
optionList = ('a', 'b', 'c')
v = tk.StringVar()
v.set(optionList[0])  # Here is the initially selected value
om = tk.OptionMenu(root, v, *optionList)
om.pack()

v.set(optionList[2]) # This one will be the final selected value 

_list = root.winfo_children()
print(len(_list))
for item in _list :
    item.set('a')
root.mainloop()

它会给出错误AttributeError: 'OptionMenu' object has no attribute 'set'

来自answer

的参考代码

0 个答案:

没有答案