检查是否禁用了Tkinter小部件(OptionMenu)

时间:2018-06-08 08:54:33

标签: python tkinter widget optionmenu

我觉得我已经在网上搜寻了一个永恒的事物,为了一些我觉得应该非常简单的事情,将我的问题重复了一千次。

我想知道是否有办法检查Tkinter Widget是否处于活动状态(不是灰色/禁用)。我有一组开始禁用的OptionMenus,当他们点击复选框时配置为state=ACTIVE,以便用户可以选择他们想要使用的OptionMenus。

当我尝试"提交" OptionMenus中的字段,我只想要那些ACTIVE的字段。我已经尝试了if OptionMenu.state == ACTIVE但是我得到一个错误,即OptionMenu没有属性状态,即使我之前配置了它。

以下是我的代码示例:

from tkinter import *    

class Application(Frame):
    # Initializing the window and the required variables
    def __init__(self, master=None):
        Frame.__init__(self, master)
        self.checkbox_in_use = BooleanVar(self, False)
        self.checkbox = Checkbutton(self, text="check", 
                                    var=self.checkbox_in_use, 
                                    command=self.check_change
        self.checkbox.grid(row=0, column=1, sticky='W')

        self.menu = OptionMenu(title_setting,
                               "Menu",
                               "Menu",
                               ["Menu1", "Menu2"])
        self.menu.grid(row=1, column=1)
        self.menu.config(state=DISABLED)

        submit = Button(self, text="submit", 
                        command=self.submit_function)
        submit.grid(row=2, column=0)

        self.master = master
        self.init_window()

    # Initialize the window
    def init_window(self):
        self.master.title("Example")

        self.pack(fill=BOTH, expand=1)


    def check_change(self):
        if self.checkbox_in_use.get():
            self.menu.config(state=ACTIVE)
        else:
            self.menu.config(state=DISABLED)

    def submit_function(self):
        # This is the part I want to do something with.
        if self.menu.state == ACTIVE:
            print("You are good to go! Do the stuff.")


root = Tk()

root.geometry("400x300")

app = Application(root)

root.mainloop()

感谢您的回复。

1 个答案:

答案 0 :(得分:2)

您需要的只是cget()self.menu.cget('state')会做到这一点。

那就是说我想在你的代码中指出其他一些东西。

Application课程在开始时已经有__init__,所以为什么要使用:

# Initialize the window
def init_window(self):
    self.master.title("Example")
    self.pack(fill=BOTH, expand=1)

你真的不应该从帧类中包装帧,而是在调用类时。打包也不会在这里工作会引发错误。请改为:app = Application(root).grid()

查看下面重新格式化的示例(使用cget())。

from tkinter import *    

class Application(Frame):

    def __init__(self, master=None):
        Frame.__init__(self, master)
        self.master.title("Example")
        self.checkbox_in_use = BooleanVar(self, False)
        self.checkbox = Checkbutton(self, text="check", var=self.checkbox_in_use, command=self.check_change)
        self.checkbox.grid(row=0, column=1, sticky='W')

        self.menu = OptionMenu(master,"Menu","Menu",["Menu1", "Menu2"])
        self.menu.grid(row=1, column=1)
        self.menu.config(state=DISABLED)

        Button(self, text="submit", command=self.submit_function).grid(row=2, column=0)

    def check_change(self):
        if self.checkbox_in_use.get():
            self.menu.config(state=ACTIVE)
        else:
            self.menu.config(state=DISABLED)

    def submit_function(self):
        print(self.menu.cget('state'))


root = Tk()
root.geometry("400x300")
app = Application(root).grid()
root.mainloop()