python - Tkinter 在文本中插入 json 数据

标签 python tkinter

我有一个 json 文件,其中包含姓名、电子邮件地址、姓氏等。 我试图将数据放入 tkinter 中的文本框中。

我尝试使用这样的标签:

with open('file.json','r') as inside:
    data = json.load(inside)

Label(Interface, text=data).place(x=100,y=100)

现在发生的情况是,只有 1 行包含所有信息,而且不可读,所以我决定使用文本框

Text(Interface, state='normal',height = 20, width = 60).place(x=10,y=350)
Text.insert(INSERT,data)

我现在收到错误:

TypeError: insert() missing 1 required positional argument: 'chars'

我读到它只需要 2 个参数,但我尝试了 3 个:

Text.insert(INSERT,data,"test")

我收到错误:

AttributeError: 'str' object has no attribute 'tk'

我不知道在这种情况下我做错了什么..

最佳答案

我无法重现您所说的问题。以下可运行示例似乎可以在文件中显示 JSON 数据:

import json
from tkinter import *


json_filename = 'inside.json'

Interface = Tk()

with open(json_filename, 'r') as inside:
    data = json.load(inside)

text = Text(Interface, state='normal', height=20, width=60)
text.place(x=10, y=50)
text.insert('1.0', str(data))

Interface.mainloop()

关于python - Tkinter 在文本中插入 json 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56330899/

相关文章:

python - 使用 tkinter 中的按钮更改变量

Python tkinter 绑定(bind)具有多个参数的函数

python - 使用 Tkinter 的 GUI 应用程序 - 拖放

python - 在 Python 3 和 tkinter 中使用变量调用函数

python - TKinter ListBox 项目高度

Python:循环/理解/映射中对象创建的时间与一次性

python - 从json中提取某些数据

python - 如何增加和填充 python 列表的大小?

python - numpy 排序奇怪的行为

python - 如何根据matplotlib中的颜色图对散点图进行着色?