python - _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 很陌生。请帮我解决这个问题。
解决方案
谢谢@Bryan Oakley!我的清单有None
,所以我这样做了:
if result[i][j] is not None:
self.entry.insert(END, result[i][j])
else:
self.entry.insert(END, "None")
推荐阅读
- javascript - 如何有条件地在 Vue.js 中显示工具提示?
- ruby-on-rails - Rails 中 PORO 的范围
- html - 如何消除隐藏的溢出孩子产生的额外高度?
- visual-studio-code - 引入远程 WSL 功能后 vscode 无法正常工作
- elixir - 如何使用变量中的键更新 Elixir 结构
- python - 使用散景的 Aitoff 投影
- python - 尝试使用 for 循环求和时未定义名称“总计”
- php - 文件内容未读完整
- visual-studio-code - 如何配置多个格式化程序以在 VSCode 中保存时按顺序运行?
- python - requests.exceptions.ConnectionError:无法建立新连接:0x05:连接被拒绝