我有一个 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/