Button 我的按钮不会执行在Tkinter中分配给它的命令

Button 我的按钮不会执行在Tkinter中分配给它的命令,button,tkinter,python-3.3,Button,Tkinter,Python 3.3,现在这个按钮可以做点什么了。它给出了一个错误,不会执行 class FCHSapp(tk.Frame): def __init__(self, parent, controller): tk.Frame.__init__(self, parent) LARGE_FONT=("Verdana", 30) label = tk.Label(self, text="FCHS Teacher Login", font=LARGE_FONT) label.pack(pady

现在这个按钮可以做点什么了。它给出了一个错误,不会执行

class FCHSapp(tk.Frame):
def __init__(self, parent, controller):
    tk.Frame.__init__(self, parent)
    LARGE_FONT=("Verdana", 30)
    label = tk.Label(self, text="FCHS Teacher Login", font=LARGE_FONT)
    label.pack(pady=10,padx=10)
    entry1 = tk.Entry(self, textvariable=var, width = 35, font=LARGE_FONT)
    entry1.pack()
    labelb = tk.Label(self, text="    ", font=LARGE_FONT)
    labelb.pack()
    button1 = tk.Button(self, text="Login", command=lambda: self.code_run, width = 30, font=LARGE_FONT)
    button1.pack()
    button2 = tk.Button(self, text="Print Absent/Tardy Teachers", command=lambda: controller.show_frame(PrintSC), width = 30, font=LARGE_FONT)
    button2.pack()
def code_run(self):
    user = var.get()
    for i in codes.keys():
        if user == i:
            if codes[user][1] == 0:
                import os
                from time import gmtime, strftime
                time = strftime("%H:%M:%S", gmtime())
                print(time)
                my_file = open("Login.txt", "w")
                name = codes[user][0]
                my_file.write(name)
                my_file.write(" ")
                my_file.write(time)
                my_file.close()
                os.startfile("C:/Users/ILCASA01/Desktop/Login Files/Login.txt", "print")
                codes[user][1] = 1
下面是出现的错误。我不明白这是什么意思。代码本身应该处理条目并打印名称。但当我执行时,它只会给我错误

Tkinter回调中的异常 回溯(最近一次呼叫最后一次): 文件“C:\Python33\lib\tkinter\uuuu init\uuuuu.py”,第1442行,在调用中 返回self.func(*args) 文件“C:\Users\ILCASA01\Desktop\Login Files\Loginv2.5 GUI Version.py”,第156行,在 button1=tk.Button(self,text=“Login”,command=lambda:self.code\u run(cont),宽度=30,字体=LARGE\u font) NameError:未定义全局名称“cont”


另外,我很抱歉没有让它看起来更整洁。

您至少有两个问题。第一,当函数应该是
self.code\u-run(…)
时,您将其调用为
code\u-run(self…)

其次,没有名为
cont
的变量。也许你指的是
控制器

lambda很难调试。除非别无选择,否则应该避免使用它们。在这种情况下,我会将
controller
设置为一个属性(例如:
self.controller
),并让被调用的函数使用它,而不是让它传入。然后您的按钮变成
按钮(…,command=self.code\u run)
,这将更容易调试

class FCHSapp(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        ...

code\u run
是一种实例方法吗?这应该是self.code\u run(cont)?不要为没有把它弄得更整洁而感到抱歉——花点时间让它看起来更整洁。如果你不愿意努力使你的问题易于阅读,为什么我们要花时间来回答它?这似乎是一个非常新的问题,但我应该在哪里设置属性?现在当我运行它时,它会给我一个error@Leonard10:在您的
\uuuu init\uuuuu
中,只需执行
self.controller=controller