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

Python/Tkinter中root.config属性错误求助

Fixing Your Tkinter Menu Code Issues

Hey there! No worries—typos are the #1 enemy of new (and even experienced!) programmers, and that's exactly what's tripping you up here. Let's break down the problems and fix your code step by step:

The Two Key Bugs in Your Code

  • Misspelled method name: You wrote root.conifig(menu=menu) but the correct Tkinter method is root.config() (you missed an 'g' in config).
  • Misspelled variable name: When adding the File cascade menu, you referenced subemenu but your submenu variable is actually named submenu (an extra 'e' slipped in there!).

Corrected Full Code

from Tkinter import *

def donothing():
    print "Ok ok i wont.."

root = Tk()
menu = Menu(root)
root.config(menu=menu)  # Fixed spelling here

submenu = Menu(menu)
menu.add_cascade(label="File", menu=submenu)  # Fixed variable name here

submenu.add_command(label="New project...", command=donothing)
submenu.add_command(label="save", command=donothing)
submenu.add_separator()
submenu.add_command(label="Exit", command=donothing)

root.mainloop()  # Added this critical line—starts the Tkinter window!

Quick Extra Tip

I also added root.mainloop() at the end—this is what makes your Tkinter window actually appear on screen. Without it, your code will run but you won't see anything pop up.

As you keep coding, keep an eye out for those tiny spelling mistakes—most IDEs (like VS Code or PyCharm) will highlight these errors for you, which saves a ton of head-scratching time!

内容的提问来源于stack exchange,提问作者Fraser Campbell

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 11:14:49