Python/Tkinter中root.config属性错误求助
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 isroot.config()(you missed an'g'inconfig). - Misspelled variable name: When adding the File cascade menu, you referenced
subemenubut your submenu variable is actually namedsubmenu(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
相关产品推荐
相关产品推荐

