Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python上定义命令时出现NameError_Python_Python 3.x_Tkinter_Nameerror - Fatal编程技术网

在python上定义命令时出现NameError

在python上定义命令时出现NameError,python,python-3.x,tkinter,nameerror,Python,Python 3.x,Tkinter,Nameerror,我不明白为什么我总是收到错误:NameError:name'submit\u answer'没有定义。 class Add(tk.Frame): def __init__(self, parent, controller): tk.Frame.__init__(self, parent, background="blue") button = tk.Button(self, text="Main Menu", font=LARGE_FONT, backgr

我不明白为什么我总是收到错误:
NameError:name'submit\u answer'没有定义。

class Add(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent, background="blue")
        button = tk.Button(self, text="Main Menu", font=LARGE_FONT, background="white",
                           command=lambda: controller.show_frame(Main))
        button.place(relx=0.83, rely=0.92)

        button4 = tk.Button(self, text="Start Game", font=LARGE_FONT, background="white",
                           command=self.time_start)
        button4.place(relx=0.42, rely=0.8, relheight=0.1, relwidth=0.2)

    def time_start(self):
        timelabel = tk.Label(self, text="Good luck!", font=LARGE_FONT, background="blue")
        timelabel.place(relx=0.42, rely=0.8, relheight=0.1, relwidth=0.2)

        x = int(random.uniform(1,10))
        y = int(random.uniform(50,100))
        z = int(random.uniform(10,50))
        qlabel = tk.Label(self, text= (x,"+",y,"+",z,"=",), font=LARGE_FONT, bg="blue")
        qlabel.pack()

##        entrylabel = tk.Label(self, text="Answer:", font=LARGE_FONT, background="blue")
##        entrylabel.pack()

        e1 = tk.Entry(self)
        e1.pack()

        ebutton = tk.Button(self, text="Done", font=LARGE_FONT, background="white",
                            command=submit_answer)
        ebutton.pack()

    def submit_answer(self):
        a = (x+y+z)
        if int(e1.get()) == a:
            answerlabel = tk.Label(self, text="Correct!", font=LARGE_FONT, background="blue")
            answerlable.pack()
        else:
            answerlabel = tk.Label(self, text="Incorrect!", font=LARGE_FONT, background="blue")
            answerlable.pack()

        self.label = tk.Label(self, text="", width=10, font=LARGE_FONT)
        self.label.place(relx=0.1, rely=0.1)
        self.remaining = 0
        self.countdown(5)

为了调用类中应该使用的方法
submit\u-answer
self.submit\u-answer

所以这一行应该改为:

ebutton = tk.Button(self, text="Done", font=LARGE_FONT, background="white",
                        command=self.submit_answer)

在将函数submit\u answer用作tk.Button的命令参数之前,您可以在函数time\u start内定义该函数。否则您必须使用self.submit\u answer将其用作tk.Button的命令参数。谢谢!现在可以了。但是现在我得到了NameError:没有定义名称“x”。您知道为什么此命令无法识别前面的变量x、y、z吗?您可以在方法
time\u start
中将它们声明为局部变量。它们不会共享给其他方法。您可以将它们设置为类成员(
self.x=int..
),然后可以使用它们。(
a=(self.x+…)