Python Tkinter如何为Tk()容器添加菜单栏及AttributeError报错处理
问题解决
错误原因
Tkinter的Menu.add_command()方法不支持直接通过位置参数传入菜单项名称,第一个位置参数默认是接收配置字典的,你直接传入字符串'Add Client',程序会尝试对这个字符串调用items()方法解析配置,就触发了AttributeError。
你需要显式用label参数指定菜单项显示文本。
修正后完整代码
##this is the order.py file responsible for drawing ##the window and ui from tkinter import * from Business import * def add_user(): print("hello world") ##create a container to hold the components inside the frame myframe=Tk() ##create the menu bar menub=Menu(myframe,background='#111', foreground='#111') ## 修正:显式指定label参数传递菜单项名称 menub.add_command(label='Add Client',command=myframe.quit) # 补充剩余两个菜单项示例,可按需求替换command对应的方法 menub.add_command(label='Print Client Lists',command=add_user) menub.add_command(label='Exit',command=myframe.quit) myframe.config(menu=menub) myframe.mainloop()
内容的提问来源于stack exchange,提问作者user16612111
相关产品推荐
相关产品推荐

