Tkinter未知选项-菜单

2024-10-04 01:34:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我不断地发现错误:

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

我在OSX10.8上使用Python3。为什么会出现未知选项错误?


Tags: selfmasteraddhelloapplicationtkinterdefframe
1条回答
网友
1楼 · 发布于 2024-10-04 01:34:36
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()

相关问题 更多 >