【问题标题】:Tkinter Unknown Option -menuTkinter 未知选项 -menu
【发布时间】:2014-04-02 12:40:33
【问题描述】:

我不断收到错误:

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

我在 OS X 10.8 上使用 python 3。为什么会出现未知选项错误?

【问题讨论】:

    标签: python python-3.x tkinter


    【解决方案1】:
    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()
    

    【讨论】:

      猜你喜欢
      • 2020-05-07
      • 2022-09-29
      • 1970-01-01
      • 1970-01-01
      • 2018-03-29
      • 2021-01-17
      • 2012-08-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多