Home > Enterprise >  How can i exception handle through a lambda function? [tkinter]
How can i exception handle through a lambda function? [tkinter]

Time:04-11

College student. I have a bit of code here, but i can't seem to retrieve the .get() user Entries from the lambda function. As a result, i can't do any exception handling. The only catch i could manage was checking to make sure there was no repeat usernames in the database. I tried returning the .get() variables to the function, but to no avail. Here is the snippet of the code i am referring to:

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)

Here, sql is just a class with some methods relating to a database, such as: creating connection, cursor and query. I have not put it here in case it is redundant, but will happily drop it where needed.

Thanks to anyone that can help.

CodePudding user response:

The simplest solution is to not use lambda. Create a proper function, and it will be easier to write, easier to understand, and easier to debug. In this specific case, lambda is doing nothing but making your code more complicated than it needs to be.

In this specific case, just move the calls to get inside of the submit_details function.

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()
    ...

If you prefer to leave submit_details the way that it is, create an intermediate function to get the data from the UI before passing it to submit_details:

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)

  • Related