错误按钮未定义?

时间:2013-08-09 13:22:35

标签: python nameerror

这是我的代码:

import sys
from tkinter import *


def next_screen(names):
    for widget in names:
        widget.place_forget()   

def forget_page1():
    widgets = [mLabel1, button]
    next_screen(widgets)

mGui = Tk ()

mGui.geometry("600x600+545+170")
mGui.title("MyMathDictionary")
mLabel1 = Label (text = "Welcome to MyMathDictionary. Press Next to continue.",
                 fg = "blue",bg = "white")
mLabel1.place (x= 150,y = 200)

button = Button (text = "Next", command = forget_page1 ())
button.place(x = 275,y = 230)

mGui.mainloop()

它告诉我:

Traceback (most recent call last):   File
"C:\Python33\Projects\MyMathDictionary.py", line 24, in <module>
    button = Button (text = "Next", command = forget_page1 (mLabel,button)) NameError: name 'button' is not defined

此错误消息的含义是什么?

1 个答案:

答案 0 :(得分:1)

更改此行代码:

button = Button (text = "Next", command = forget_page1 ())

对此:

button = Button (text = "Next", command = forget_page1)

您的问题是您在加载窗口之前调用了forget_page1

此外,正如评论已经说过,您的错误与您的代码不同。但是,为了以防万一,我也会很快过去。如果要将参数发送到按钮的命令功能,则需要使用lambda

button = Button(command = lambda: func(arg1, arg2))