You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.27 04:45:04