Tkinter未知选项-menu

Dea*_*ean 0 python tkinter python-3.x

我一直收到错误:

_tkinter.TclError:未知选项"-menu"

我的MWE看起来像:

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()
Run Code Online (Sandbox Code Playgroud)

我在OS X 10.8上使用python 3.为什么我收到未知选项错误?

Kev*_*vin 6

self.config(menu=self.menuBar)
Run Code Online (Sandbox Code Playgroud)

menu不是有效的配置选项Frame.

也许你的意思是继承而来Tk

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()
Run Code Online (Sandbox Code Playgroud)