NameError:name' Menu'没有定义

时间:2016-03-04 03:04:31

标签: python-3.x tkinter

我正在尝试为我的GUI制作菜单栏。我收到这个错误:

`NameError: name 'Menu' is not defined`

我不确定为什么会这样,有人可以帮帮我吗?道歉,我是python和tkinter的初学者。

from tkinter import Tk, BOTH
from tkinter.ttk import Frame, Button, Style


class Example(Frame):

    def __init__(self, parent):
        Frame.__init__(self, parent)

        self.parent = parent
        self.parent.title("xcal file.ics")
        self.pack(fill=BOTH, expand=1)
        self.centerWindow()
        self.createMenuBar()


    def centerWindow(self):

        w = 500
        h = 500

        sw = self.parent.winfo_screenwidth()
        sh = self.parent.winfo_screenheight()

        x = (sw - w)/2
        y = (sh - h)/2
        self.parent.geometry('%dx%d+%d+%d' % (w, h, x, y))

    def createMenuBar(self):
        menubar = Menu(Frame) #ERROR
        menubar.add_command(label="Hello!", command=hello)
        Frame.config(menu=menubar)

def main():

    root = Tk()
    ex = Example(root)
    root.mainloop()


if __name__ == '__main__':
    main()

1 个答案:

答案 0 :(得分:1)

Menu位于模块tkinter中,因此您需要导入它:

from tkinter import Tk, BOTH, Menu