user15256253
user15256253

Reputation:

_tkinter.TclError: wrong # args: should be ".!entry4 insert index text"

I'm trying to list all the data i got from my database using tkinter.
I'm following this post: https://www.geeksforgeeks.org/create-table-using-tkinter/

I got this erorr:

File "/Users/nobu/WorkSpace/Python/CommandLineApps/guineapig/guineapig/gui_tkinter.py", line 18, in __init__
    self.entry.insert(END, result[i][j])
File "/usr/local/Cellar/[email protected]/3.9.7/Frameworks/Python.framework/Versions/3.9/lib/python3.9/tkinter/__init__.py", line 3056, in insert
    self.tk.call(self._w, 'insert', index, string)
_tkinter.TclError: wrong # args: should be ".!entry4 insert index text"

Here's my code:

from tkinter import *
import mysql.connector
import utils


class Table:
    def __init__(self,root, result):
        # code for creating table
        total_rows = len(result)
        total_columns = len(result[0])
        for i in range(total_rows):
            for j in range(total_columns):    
                self.e = Entry(root, width=20, fg='white')
                self.e.grid(row=i, column=j)
                self.e.insert(END, result[i][j]) # <- Here I got an error

def get_all():
    connection, cursor = utils.connect_db()
    with connection:
        root = Tk()
        cursor.execute("SELECT * FROM item ORDER BY item_id DESC")
        result = cursor.fetchall()
        if len(result) == 0:
            label = Label(text="There are no items. Please create one.")
        else:
            table = Table(root, result)
            root.mainloop()

I'm very new to tkinter. Please help me solve this issue.

Upvotes: 0

Views: 1294

Answers (1)

user15256253
user15256253

Reputation:

Thank you @Bryan Oakley! My list has None, so I did this:

if result[i][j] is not None:
    self.entry.insert(END, result[i][j])
else:
    self.entry.insert(END, "None")

Upvotes: 3

Related Questions