2014-04-02 78 views
0

我不斷收到錯誤:Tkinter的未知選項 - 菜單

_tkinter.TclError: unknown option "-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() 

我OS X 10.8使用python 3.爲什麼我收到未知選項錯誤?

回答

6
self.config(menu=self.menuBar) 

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()