Tkinter中点击Help菜单About后Text组件无法显示插入文本
Tkinter Help菜单点击无响应问题排查
你的代码存在两个关键问题,导致点击About后Text组件没有显示内容:
- 命名冲突:
help是Python的内置函数,你用它作为自定义函数名会覆盖内置函数,导致菜单绑定的逻辑完全不符合预期。 - 定义顺序错误:执行
helpmenu.add_command(label="About", command=help)时,你自定义的help函数还未定义,此时绑定的是Python内置的help函数,根本不会执行文本插入的代码。
另外原代码还遗漏了将菜单挂载到窗口的关键步骤,修正后的完整代码如下:
from tkinter import * root = Tk() root.geometry("400x300") # 先定义自定义函数,确保绑定命令时函数已存在 def show_about_info(): info.insert(INSERT, "Designed:" + "\n") # 创建菜单 menubar = Menu(root) helpmenu = Menu(menubar) # 绑定修改后的自定义函数 helpmenu.add_command(label="About", command=show_about_info) menubar.add_cascade(label="Help", menu=helpmenu) # 配置Text组件 info = Text(root, bd=2, wrap="word") info.tag_configure("center", justify='center') info.pack(side=LEFT, fill=BOTH, expand=True) # 将菜单挂载到窗口 root.config(menu=menubar) root.mainloop()
内容的提问来源于stack exchange,提问作者user19333121
相关产品推荐
相关产品推荐

