Click here to Skip to main content
15,901,718 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to create a Custom Tkinter app with CTK table, I have an errored when I insert data. I want to insert the data at the end of each row of table, but I didn't know what is the row number should be. My code is as below:

What I have tried:

Python
def data_insert():
    # Read the data from the entry boxes
    my_name = name.get()
    my_class = class1.get()
    my_mark = mark1.get()
    my_gender = gender.get()
    my_address = address.get("1.0", "end")
    my_hostel = hostel.get()
    
    # verification of input
    varify = True
    if my_name == '' or my_class == ''  or my_address == '':
        CTkMessagebox(title="Error", message="All entries cann't leave blank", icon="cancel")
        varify = False
    
    else:
        varify = True
        
        # insert into SQLite
        sql = "INSERT INTO student_address (name, class, mark, gender, address, hostel) \
            VALUES(:name, :class, :mark, :gender, :address, :hostel)"
    
        my_data = {
             "name": my_name,
             "class": my_class,
             "mark": my_mark,
             "gender": my_gender,
             "address": my_address,
             "hostel": my_hostel,
        }
    
        with engine.connect() as my_conn:
            content = my_conn.execute(text(sql), my_data)
            
            my_conn.commit()
        
            table.add_row(END, my_data)
        # print(my_data)
            clear_entries()

Error incurred
TypeError: 'dict' object cannot be interpreted as an integer
Posted

row_id = content.lastrowid
new_row_values = [row_id, my_name, my_class, my_mark,
                  my_gender, my_hostel, my_address]
table.add_row(new_row_values)
 
Share this answer
 
You have your parameters in the wrong order in the call to add_row as shown in the source code at CTkTable/CTkTable/ctktable.py at main · Akascape/CTkTable · GitHub[^]:
Python
table.add_row(END, my_data)

The data dictionary must be the first parameter, thus:
Python
table.add_row(my_data, END)

I also noticed that the documentation at CTkTable · PyPI[^] has the parameters the wrong way round.
 
Share this answer
 
v2
You shouldn't need to specify END in add_row. As far as I can tell, from the documentation, you should be able to do this to add the data to the end.
Python
table.add_row(my_data)
Having looked at your code, you're trying to add a dictionary to something that's expecting a list. Do this instead:
Python
table.add_row([my_name, my_class, my_mark, my_gender, my_address, my_hostel])
 
Share this answer
 
v2
Comments
Member 15783420 3-Apr-24 4:37am    
File "/home/runner/CTK-CRUD1/.pythonlibs/lib/python3.11/site-packages/customtkinter/windows/widgets/ctk_button.py", line 554, in _clicked
self._command()
File "/home/runner/CTK-CRUD1/main.py", line 61, in data_insert
table.add_row(my_data)
File "/home/runner/CTK-CRUD1/.pythonlibs/lib/python3.11/site-packages/CTkTable/ctktable.py", line 238, in add_row
self.draw_table(**kwargs)
File "/home/runner/CTK-CRUD1/.pythonlibs/lib/python3.11/site-packages/CTkTable/ctktable.py", line 119, in draw_table
value = self.values[i][j]
~~~~~~~~~~~~~~^^^
KeyError: 0

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900