我一直在修改前同事的代码,并试图在解决问题的同时添加功能。我添加了一个顶层菜单(文件,帮助等),但是有一个GUIStateSaver可以保存“ widget.state”,而且我似乎无法使它们一起工作。我总是收到此错误:“菜单”对象没有属性“状态”。如果我禁用状态保护程序,则一切正常。
这是我的临时测试菜单代码:
# 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)
这是引发错误的代码部分:
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
我需要在菜单代码中添加一些内容吗?我需要在特定位置放入菜单代码?