按钮现在会做一些事情 . 它会出错并且不会执行 .
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__init__.py",第1442行,在 call 中返回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_run(cont),width = 30,font = LARGE_FONT)NameError:未定义全局名称'cont'
另外,我很抱歉没有让它看起来更整洁 .
1 回答
你至少有两个问题 . 一,当它应该是
self.code_run(...)
时,你将函数调用为code_run(self, ...)
.其次,您没有名为
cont
的变量 . 也许你的意思controller
?lambdas很难调试 . 除非别无选择,否则应避免使用它们 . 在这种情况下,我会使
controller
属性(例如:self.controller
)并让被调用的函数使用它而不是传入它 . 然后你的按钮变为Button(..., command=self.code_run)
,这将更容易调试 .