我正在尝试为我的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()
答案 0 :(得分:1)
Menu
位于模块tkinter
中,因此您需要导入它:
from tkinter import Tk, BOTH, Menu