首页 > 解决方案 > _tkinter.TclError: 错误# args: 应该是“.!entry4 插入索引文本”

问题描述

我正在尝试使用 tkinter 列出我从数据库中获取的所有数据。
我正在关注这篇文章:https ://www.geeksforgeeks.org/create-table-using-tkinter/

我得到了这个错误:

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/python@3.9/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"

这是我的代码:

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

我对 tkinter 很陌生。请帮我解决这个问题。

标签: pythontkinter

解决方案


谢谢@Bryan Oakley!我的清单有None,所以我这样做了:

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

推荐阅读