我如何通过 lambda 函数处理异常? [tkinter]
How can i exception handle through a lambda function? [tkinter]
大学生。
我这里有一些代码,但我似乎无法从 lambda 函数中检索 .get() 用户条目。
结果,我不能做任何异常处理。我唯一能解决的问题是检查以确保数据库中没有重复的用户名。我尝试将 .get() 变量返回给函数,但无济于事。这是我所指的代码片段:
def second_win():
window = Tk()
window.title("Please enter your personal details")
window.geometry('500x500')
Label(window, text = 'Create Username').grid(row = 0, column = 0)
Label(window, text=" First Name").grid(row=1, column = 0 )
Label(window, text=" Last Name").grid(row=2,column = 0)
Label(window, text = "Age").grid(row = 3,column = 0)
Label(window, text = "Height (m)").grid(row = 4,column = 0)
Label(window, text = "Weight(kg)").grid(row = 5,column = 0)
UserName = Entry(window, width = 25)
UserName.grid(row = 0, column =1)
User_FirstName = Entry(window, width = 25)
User_FirstName.grid(row=1, column = 1)
User_Surname = Entry(window, width = 25)
User_Surname.grid(row=2, column = 1 )
User_Age = Entry(window, width = 25)
User_Age.grid(row = 3, column = 1)
User_Height = Entry(window, width = 25)
User_Height.grid(row = 4, column = 1)
User_Weight = Entry(window, width = 25)
User_Weight.grid(row = 5, column = 1)
Button(window, text='Quit', command=window.quit).grid(row=10, column=0, sticky=W, padx = 100, pady=100)
Button(window, text='Submit', command= lambda: submit_details([ UserName.get(), User_FirstName.get(), User_Surname.get(), User_Age.get(), User_Height.get(),User_Weight.get()])).grid()
def submit_details(details):
user_name_list = cursor.execute(''' SELECT UserName FROM GetUserDataInput''')
if details[0] in [username[0] for username in user_name_list]:
messagebox.showerror("Error", "User Already Exists")
else:
sql = '''INSERT INTO GetUserDataInput(UserName, FirstName, LastName, Age, Height, Weight)
VALUES(?, ?, ?, ?, ?, ?)'''
cursor.execute(sql, details)
这里,sql只是一个class,带有一些与数据库有关的方法,例如:创建连接、游标和查询。我没有把它放在这里以防它是多余的,但会很乐意把它放在需要的地方。
感谢任何能提供帮助的人。
最简单的解决方案是不使用 lambda。创建一个合适的函数,它会更容易编写,更容易理解,更容易调试。在这种特定情况下,lambda
只会让您的代码变得比它需要的更复杂。
在这种特定情况下,只需将调用移至 submit_details
函数内的 get
。
def second_win():
...
Button(window, text='Submit', command= submit_details).grid()
...
def submit_details():
username = UserName.get()
firstname = User_FirstName.get()
surname = User_Surname.get()
age = User_Age.get()
height = User_Height.get()
weight = User_weight.get()
...
如果您希望 submit_details
保持原样,请创建一个中间函数以在将数据传递给 submit_details
之前从 UI 获取数据:
def second_win():
...
Button(window, text='Submit', command=submit).grid()
...
def submit():
username = UserName.get()
firstname = User_FirstName.get()
surname = User_Surname.get()
age = User_Age.get()
height = User_Height.get()
weight = User_weight.get()
data = [username, firstname, surname, age, height, weight]
submit_details(data)
大学生。 我这里有一些代码,但我似乎无法从 lambda 函数中检索 .get() 用户条目。 结果,我不能做任何异常处理。我唯一能解决的问题是检查以确保数据库中没有重复的用户名。我尝试将 .get() 变量返回给函数,但无济于事。这是我所指的代码片段:
def second_win():
window = Tk()
window.title("Please enter your personal details")
window.geometry('500x500')
Label(window, text = 'Create Username').grid(row = 0, column = 0)
Label(window, text=" First Name").grid(row=1, column = 0 )
Label(window, text=" Last Name").grid(row=2,column = 0)
Label(window, text = "Age").grid(row = 3,column = 0)
Label(window, text = "Height (m)").grid(row = 4,column = 0)
Label(window, text = "Weight(kg)").grid(row = 5,column = 0)
UserName = Entry(window, width = 25)
UserName.grid(row = 0, column =1)
User_FirstName = Entry(window, width = 25)
User_FirstName.grid(row=1, column = 1)
User_Surname = Entry(window, width = 25)
User_Surname.grid(row=2, column = 1 )
User_Age = Entry(window, width = 25)
User_Age.grid(row = 3, column = 1)
User_Height = Entry(window, width = 25)
User_Height.grid(row = 4, column = 1)
User_Weight = Entry(window, width = 25)
User_Weight.grid(row = 5, column = 1)
Button(window, text='Quit', command=window.quit).grid(row=10, column=0, sticky=W, padx = 100, pady=100)
Button(window, text='Submit', command= lambda: submit_details([ UserName.get(), User_FirstName.get(), User_Surname.get(), User_Age.get(), User_Height.get(),User_Weight.get()])).grid()
def submit_details(details):
user_name_list = cursor.execute(''' SELECT UserName FROM GetUserDataInput''')
if details[0] in [username[0] for username in user_name_list]:
messagebox.showerror("Error", "User Already Exists")
else:
sql = '''INSERT INTO GetUserDataInput(UserName, FirstName, LastName, Age, Height, Weight)
VALUES(?, ?, ?, ?, ?, ?)'''
cursor.execute(sql, details)
这里,sql只是一个class,带有一些与数据库有关的方法,例如:创建连接、游标和查询。我没有把它放在这里以防它是多余的,但会很乐意把它放在需要的地方。
感谢任何能提供帮助的人。
最简单的解决方案是不使用 lambda。创建一个合适的函数,它会更容易编写,更容易理解,更容易调试。在这种特定情况下,lambda
只会让您的代码变得比它需要的更复杂。
在这种特定情况下,只需将调用移至 submit_details
函数内的 get
。
def second_win():
...
Button(window, text='Submit', command= submit_details).grid()
...
def submit_details():
username = UserName.get()
firstname = User_FirstName.get()
surname = User_Surname.get()
age = User_Age.get()
height = User_Height.get()
weight = User_weight.get()
...
如果您希望 submit_details
保持原样,请创建一个中间函数以在将数据传递给 submit_details
之前从 UI 获取数据:
def second_win():
...
Button(window, text='Submit', command=submit).grid()
...
def submit():
username = UserName.get()
firstname = User_FirstName.get()
surname = User_Surname.get()
age = User_Age.get()
height = User_Height.get()
weight = User_weight.get()
data = [username, firstname, surname, age, height, weight]
submit_details(data)