菜鸟没有在python中定义

时间:2017-07-14 16:44:48

标签: python tkinter

我正在尝试使用python中的tkinter库创建一个程序,但它显示错误显示---

NameError:name'菜单栏'未定义

import tkinter
import sys


def hey():
    print("hello")


def myNew():
    mlabel = Label(root,text="yo").pack()



root = tkinter.Tk()
root.title("Wizelane")

root.geometry('400x80+350+340')
filemenu = tkinter.Menu(menubar, tearoff=0)
filemenu.add_command(label="New",command=myNew)

label = tkinter.Label(root,text="say hello")
label.pack()
hello = tkinter.Button(root,text="hello",command=hey)
hello.pack()
root.mainloop()

1 个答案:

答案 0 :(得分:0)

You are missing some important parts here.

You need to configure the menu first and you also need to add the cascade label.

Take a look at this code.

import tkinter

def hey():
    print("hello")

def myNew():
    # you forgot to use tkinter.Label here.
    mlabel = tkinter.Label(root, text="yo").pack()


root = tkinter.Tk()
root.title("Wizelane")
root.geometry('400x80+350+340')

my_menu = tkinter.Menu(root)

# configure root to use my_menu widget.
root.config(menu = my_menu) 

# create a menu widget to place on the menubar
file_menu = tkinter.Menu(my_menu, tearoff=0)

# add the File cascade option for drop down use
my_menu.add_cascade(label = "File", menu = file_menu)

# then add the command you want to add as a File option.
file_menu.add_command(label="New", command = myNew)

label = tkinter.Label(root, text="say hello")
label.pack()

hello = tkinter.Button(root, text="hello", command = hey)
hello.pack()

root.mainloop()