所以我有这段代码:
for p in self.ProductNames:
OptionMenuVar = StringVar()
menu = OptionMenu(self.FrameProducts, OptionMenuVar, *self.ProductNames)
OptionMenuVar.set(p)
AgeVar = StringVar()
AgeEntry = Entry(self.FrameProducts,width=15,textvariable=AgeVar,state="readonly",justify=CENTER)
生成此UI:
如何跟踪OptionMenuVar
中的更改并根据所选值更新AgeVar
?
我看过The Variable Classes。我想我知道如何追踪OptionMenuVar
中的变化,但我仍然不知道如何:
AgeVar
醇>
答案 0 :(得分:2)
所以这是做到这一点的方法:
def OnOptionMenuChnage(omv,av, *pargs):
print omv.get(), av.get()
# do more. set av value based on omv value
OptionMenuVar.trace("w", lambda *pargs: OnOptionMenuChnage(OptionMenuVar,AgeVar, *pargs))
for p in self.ProductNames:
OptionMenuVar = StringVar()
menu = OptionMenu(self.FrameProducts, OptionMenuVar, *self.ProductNames)
OptionMenuVar.set(p)
AgeVar = StringVar()
AgeEntry = Entry(self.FrameProducts,width=15,textvariable=AgeVar,state="readonly",justify=CENTER)
def OnOptionMenuChnage(omv,av, *pargs):
print omv.get(), av.get()
# do more. set av value based on omv value
OptionMenuVar.trace("w", lambda *pargs: OnOptionMenuChnage(OptionMenuVar,AgeVar, *pargs))
所有归功于Marcin answer。