Getting NameError: free variable referenced before assignment, but cannot see where I've done so
Getting NameError: free variable referenced before assignment, but cannot see where I've done so
我正在尝试为我的应用程序创建一个基本的登录框架(根本不用担心它会很复杂,它适用于 A Level 项目)。在 checklogin 函数中,我试图检索用户输入的用户名,并查看它是否已经在数据库中,稍后将添加一些以检查密码是否匹配。但是,我不断收到错误消息“NameError:在封闭范围内赋值之前引用了自由变量 'userentry'”。看这个错误,它说通常的原因是因为 'userentry' 在同一函数中被重新分配给稍后。但是,我正在努力寻找这种情况。任何帮助将不胜感激。
class Login(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
self.configure(background="lightgreen")
def check_login():
sql = "SELECT Password FROM Player WHERE Username IN (?)"
parameters = (userentry.get())
cursor.execute(sql, parameters)
print(cursor.fetchall())
for col in range(5):
self.grid_columnconfigure(col, minsize=50)
for row in range(7):
self.grid_rowconfigure(row, minsize=60)
titlelbl = tk.Label(self,
text="Please enter your username and password",
font = "Verdana 20 bold",
fg="black",
bg="lightgreen")
titlelbl.grid(column=1,
row=0,
columnspan=3)
usernamelbl = tk.Label(self,
text="Username:",
font="Verdana 14",
bg="lightgreen")
usernamelbl.grid(column=1,
row=2)
passwordlbl = tk.Label(self,
text="Password:",
font="Verdana 14",
bg="lightgreen")
passwordlbl.grid(column=1,
row=4)
signupbtn = tk.Button(self,
text="Sign Up",
fg="lightgreen",
bg="darkgreen",
height="3",
width="12",
command = lambda: controller.show_frame("SignUp"))
signupbtn.grid(column=3,
row=6)
loginbtn = tk.Button(self,
text="Log In",
fg="lightgreen",
bg="darkgreen",
height="3",
width="12",
command = check_login())
loginbtn.grid(column=1,
row=6)
userentry = tk.Entry(self)
userentry.grid(column=3,
row=2)
passentry = tk.Entry(self,
show="*")
passentry.grid(column=3,
row=4)
在此代码中
loginbtn = tk.Button(self,
text="Log In",
fg="lightgreen",
bg="darkgreen",
height="3",
width="12",
command = check_login())
将 command = check_login()
更改为 command = check_login
(不带括号)。
check_login
是函数对象。 check_login()
调用 函数对象并计算出函数的 return 值。
括号告诉 Python 立即调用该函数。这里,当tk.Button
被定义时,userentry
变量还没有被定义。这就是为什么在调用 check_login
时得到 NameError
的原因。
因此,如果您改为使用 command = check_login
,则 tk.Button
接收函数对象 check_login
,然后可以在按下按钮时调用它。
我正在尝试为我的应用程序创建一个基本的登录框架(根本不用担心它会很复杂,它适用于 A Level 项目)。在 checklogin 函数中,我试图检索用户输入的用户名,并查看它是否已经在数据库中,稍后将添加一些以检查密码是否匹配。但是,我不断收到错误消息“NameError:在封闭范围内赋值之前引用了自由变量 'userentry'”。看这个错误,它说通常的原因是因为 'userentry' 在同一函数中被重新分配给稍后。但是,我正在努力寻找这种情况。任何帮助将不胜感激。
class Login(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
self.configure(background="lightgreen")
def check_login():
sql = "SELECT Password FROM Player WHERE Username IN (?)"
parameters = (userentry.get())
cursor.execute(sql, parameters)
print(cursor.fetchall())
for col in range(5):
self.grid_columnconfigure(col, minsize=50)
for row in range(7):
self.grid_rowconfigure(row, minsize=60)
titlelbl = tk.Label(self,
text="Please enter your username and password",
font = "Verdana 20 bold",
fg="black",
bg="lightgreen")
titlelbl.grid(column=1,
row=0,
columnspan=3)
usernamelbl = tk.Label(self,
text="Username:",
font="Verdana 14",
bg="lightgreen")
usernamelbl.grid(column=1,
row=2)
passwordlbl = tk.Label(self,
text="Password:",
font="Verdana 14",
bg="lightgreen")
passwordlbl.grid(column=1,
row=4)
signupbtn = tk.Button(self,
text="Sign Up",
fg="lightgreen",
bg="darkgreen",
height="3",
width="12",
command = lambda: controller.show_frame("SignUp"))
signupbtn.grid(column=3,
row=6)
loginbtn = tk.Button(self,
text="Log In",
fg="lightgreen",
bg="darkgreen",
height="3",
width="12",
command = check_login())
loginbtn.grid(column=1,
row=6)
userentry = tk.Entry(self)
userentry.grid(column=3,
row=2)
passentry = tk.Entry(self,
show="*")
passentry.grid(column=3,
row=4)
在此代码中
loginbtn = tk.Button(self,
text="Log In",
fg="lightgreen",
bg="darkgreen",
height="3",
width="12",
command = check_login())
将 command = check_login()
更改为 command = check_login
(不带括号)。
check_login
是函数对象。 check_login()
调用 函数对象并计算出函数的 return 值。
括号告诉 Python 立即调用该函数。这里,当tk.Button
被定义时,userentry
变量还没有被定义。这就是为什么在调用 check_login
时得到 NameError
的原因。
因此,如果您改为使用 command = check_login
,则 tk.Button
接收函数对象 check_login
,然后可以在按下按钮时调用它。