Code Bug Fix: Setting widget state of “Menu”

Original Source Link

I’ve been modifying an ex-coworkers code and trying to fix problems while also adding functionality. I added a toplevel menu (File, help, etc) but there is a GUIStateSaver that saves “widget.state” and I can’t seem to get them to work together; I always get this error: ‘Menu’ object has no attribute ‘state’. If I disable the state-saver then everything works fine.

Here is my temporary test menu code:

        # File menu
        self.menubar = Menu(self)
        self.filemenu = Menu(self.menubar, tearoff=0)
        self.filemenu.add_command(label="Open",state="normal") # , command)
        self.filemenu.add_command(label="Save",state="disabled") # , command)
        self.filemenu.add_separator()
        self.filemenu.add_command(label="Exit", command=self.quit,state="normal")
        self.menubar.add_cascade(label="File", menu=self.filemenu,state="normal")

        def about():
            tkinter.messagebox.showinfo(title="2020",message="Test")
        def help():
            tkinter.messagebox.showerror(title="No", message="No help.")

        self.helpmenu = Menu(self.menubar, tearoff=0)
        self.helpmenu.add_command(label="About", command=about)
        self.helpmenu.add_command(label="Help", command=help)
        self.menubar.add_cascade(label="Help", menu=self.helpmenu)
        self.config(menu=self.menubar)

Here is the section of code that throws the error:

def save_current_state(self):
        """Update the saved state of the GUI."""
        for frame in self.main_window_.winfo_children():
            for widget in frame.winfo_children():

                # Get the state and ID of the widget. For the state, we
                # only care whether the widget is enabled or disabled.
                widget_id = str(widget.winfo_id())
                widget_states = widget.state()    # <---- That's where it stops.
                if "disabled" in widget_states:
                    widget_state = "disabled"
                else:
                    widget_state = "!disabled"

                self.state_dict_[widget_id] = widget_state

Is there something I need to add to the menu code? Somewhere specific I need to put the menu code?

Tagged : / / /

Leave a Reply

Your email address will not be published. Required fields are marked *