Tkinter 未知选项 -menu
我一直收到这个错误:
_tkinter.TclError: 未知选项 "-menu"
我的最小可重现示例是:
from tkinter import *
def hello():
print("hello!")
class Application(Frame):
def createWidgets(self):
self.menuBar = Menu(master=self)
self.filemenu = Menu(self.menuBar, tearoff=0)
self.filemenu.add_command(label="Hello!", command=hello)
self.filemenu.add_command(label="Quit!", command=self.quit)
def __init__(self, master):
Frame.__init__(self, master)
self.pack()
self.createWidgets()
self.config(menu=self.menuBar)
if __name__ == "__main__":
root = Tk()
ui = Application(root)
ui.mainloop()
我在使用 OS X 10.8 和 Python 3。为什么会出现这个未知选项的错误呢?
1 个回答
6
self.config(menu=self.menuBar)
from tkinter import *
def hello():
print("hello!")
class Application(Tk):
def createWidgets(self):
self.menuBar = Menu(master=self)
self.filemenu = Menu(self.menuBar, tearoff=0)
self.filemenu.add_command(label="Hello!", command=hello)
self.filemenu.add_command(label="Quit!", command=self.quit)
self.menuBar.add_cascade(label="File", menu=self.filemenu)
def __init__(self):
Tk.__init__(self)
self.createWidgets()
self.config(menu=self.menuBar)
if __name__ == "__main__":
ui = Application()
ui.mainloop()
这个提示的意思是,menu
这个选项在 Frame
里是不被认可的,也就是说你不能在 Frame
这个地方使用 menu
。
也许你是想从 Tk
这个地方继承一些东西呢?