检查选择了哪个tkinter OptionMenu项目

时间:2014-09-19 15:31:07

标签: python tkinter

如何检查用户是否已选择"其他"从hopOptions选择,然后启用otherEntry,如果他们这样做?如果他们选择其中一个选项,则再次禁用它。

    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)
            self.hopOptions.grid(row=0, column=2, sticky=EW)

            self.otherEntry = Entry(frame, state=DISABLED)
            self.otherEntry.grid(row=0, column=1, sticky=EW)

    root = Tk()

    app = Interface(root)

    root.mainloop()

2 个答案:

答案 0 :(得分:2)

将选项菜单绑定到命令并向您的类添加另一个方法。只要在菜单中更改了选项,该命令将以值作为参数运行类方法。在那里,您可以进行验证以更新otherEntry小部件。另外我建议你不要做from Tkinter import *,因为它看起来就是你所做的。通常,导入整个包可能与您的命名空间冲突。这应该适合您的需求:

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()

答案 1 :(得分:2)

作为iChar的command方法的替代方法,使用selectedHop.trace注册将在所选项目发生更改时调用的函数。

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.selectedHop.trace("w", self.selected_hop_changed)
        self.hopOptions = OptionMenu(frame, self.selectedHop, *hops)
        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 selected_hop_changed(self, *args):
        value = self.selectedHop.get()
        if value == "Other":
            self.otherEntry.config(state=NORMAL)
        else:
            self.otherEntry.config(state=DISABLED)


root = Tk()

app = Interface(root)

root.mainloop()