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

时间:2015-09-23 11:54:27

标签: 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=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回调中的异常 Traceback(最近一次调用最后一次):   文件" C:\ Python33 \ lib \ tkinter__init __。py",第1442行,调用     return self.func(* args)   文件" C:\ Users \ ILCASA01 \ Desktop \ Login Files \ Loginv2.5 GUI Version.py",第156行,in     button1 = tk.Button(self,text =" Login&#34 ;, command = lambda:self.code_run(cont),width = 30,font = LARGE_FONT) NameError:全局名称' cont'未定义

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

1 个答案:

答案 0 :(得分:0)

你至少有两个问题。一,当code_run(self, ...)self.code_run(...)时,您将该函数称为cont

其次,您没有名为controller的变量。也许你的意思是controller

lambdas很难调试。除非别无选择,否则应避免使用它们。在这种情况下,我会使self.controller成为一个属性(例如:Button(..., command=self.code_run))并让被调用的函数使用它而不是传入它。然后你的按钮变为class FCHSapp(tk.Frame): def __init__(self, parent, controller): tk.Frame.__init__(self, parent) self.controller = controller ... ,这将更容易调试。

SWTRenderersMessages
相关问题