如何解决“_tkinter.TclError:未知选项”?

时间:2016-09-26 09:40:51

标签: python-2.7 tkinter menu

我正在学习python tkinter,但每当我尝试编译它时都会出错:

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/dist-packages/spyderlib/ 
widgets/externalshell/sitecustomize.py", line 540, in runfile 
execfile(filename, namespace)
File "/home/jason/.spyder2/.temp.py", line 14, in <module>
menu.config(menu=menu)
File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1274, 
inconfigure
return self._configure('configure', cnf, kw)
File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1265, 
in _configure
self.tk.call(_flatten((self._w, cmd)) + self._options(cnf))
_tkinter.TclError: unknown option "-menu").

我的代码是:

from tkinter import *

def hello():
    print "hello"    

root = Tk()
menu = Menu(root)
menu.config(menu=menu)
menu.add_command(label ="new",command = hello)

root.mainloop()

1 个答案:

答案 0 :(得分:0)

您的代码有点问题。使用menu.config代替root.config,您将不会遇到任何此类错误。

有关详细信息和详细教程,请访问Tkinter Menu Widget