0

I have created a table in Tkinter and each row has a "Edit" button. When this edit button is pressed, a function has to be called:

tk.Button(actual_table_frame, text=y, command=(lambda: edit_row(retrieve_data_result[i][0])) if index == 0 else (lambda: delete_row(retrieve_data_result[i][0], current_items_tracker['first_item_id'])), font=("Arial", 12), pady=5, padx=30).grid(row=row_counter+1, column=index+7)

I have used lambda to call the function that I want, with an argument so that the edit_row function can figure out which row has to be edited. This is the argument that has to be sent to the edit_row function: retrieve_data_result[i][0]

I created each cell of the table with a for loop which iterates over a 2D list and fills the table with the data that exists in the second dimension of the list:

for i in range(len(retrieve_data_result)):
    for j in range(7):
        tk.Label(actual_table_frame, text=retrieve_data_result[i][j], font=("Arial", 12)).grid(row=row_counter+1, column=j)
                        
    for index, y in enumerate(["Edit It", "Delete It"]):
        tk.Button(actual_table_frame, text=y, command=(lambda: edit_row(retrieve_data_result[i][0])) if index == 0 else (lambda: delete_row(retrieve_data_result[i][0], current_items_tracker['first_item_id'])), font=("Arial", 12), pady=5, padx=30).grid(row=row_counter+1, column=index+7)

The first inner for-loop, places the actual data of each row, and the second inner for-loop, adds buttons to each row. The problem is that the value of retrieve_data_result[i][0] is determined when the button is pressed. Not when the data is placed in each row. for example, for the first row, I want the edit_row function to be called with 1 as the argument but since at the time of pressing the button, the outer for-loop has done its job, the i is at its maximum. So the edit_row function is called with the maximum i always. How can I solve this issue without saving each row's id in the text of the button?

Ali Safapour
  • 103
  • 1
  • 5

0 Answers0