<p>将选项菜单绑定到命令,并将另一个方法添加到类中。只要菜单中的某个选项发生更改,该命令都将以值作为参数运行类方法。您可以在那里进行验证以更新otherEntry小部件。另外,我建议不要做<code>from Tkinter import *</code>,因为看起来这就是你所做的。通常导入整个包可能与您的命名空间冲突。这应该适合您的需要:</p>
<pre><code>from Tkinter import *
class Interface():
def __init__(self, window):
frame = Frame(window)
frame.pack()
self.hopLabel = Label(frame, text="Hop:", anchor=E)
self.hopLabel.grid(row=0, column=0, sticky=EW)
hops = range(0,6)
hops.append("Other")
self.selectedHop = StringVar(frame)
self.selectedHop.set(hops[0])
self.hopOptions = OptionMenu(frame, self.selectedHop, *hops, command=self.optupdate)
self.hopOptions.grid(row=0, column=2, sticky=EW)
self.otherEntry = Entry(frame, state=DISABLED)
self.otherEntry.grid(row=0, column=1, sticky=EW)
def optupdate(self, value):
if value == "Other":
self.otherEntry.config(state=NORMAL)
else:
self.otherEntry.config(state=DISABLED)
if __name__ == "__main__":
root = Tk()
app = Interface(root)
root.mainloop()
</code></pre>