Tôi đang cố gắng tạo chương trình sẽ đọc trong chuỗi JSON thông qua GUI và sau đó sử dụng điều này để thực hiện các hàm bổ sung, trong trường hợp này phá vỡ một phương trình toán học. Tại thời điểm tôi gặp lỗi:Đọc chuỗi JSON | TypeError: chuỗi chỉ số phải là số nguyên
"TypeError: string indices must be integers"
và tôi không biết tại sao.
Các JSON Tôi cố gắng để đọc như sau:
{
"rightArgument":{
"cell":"C18",
"value":9.5,
"type":"cell"
},
"leftArgument":{
"rightArgument":{
"cell":"C3",
"value":135,
"type":"cell"
},
"leftArgument":{
"rightArgument":{
"cell":"C4",
"value":125,
"type":"cell"
},
"leftArgument":{
"cell":"C5",
"value":106,
"type":"cell"
},
"type":"operation",
"operator":"*"
},
"type":"operation",
"operator":"+"
},
"type":"operation",
"operator":"+"
}
import json
import tkinter
from tkinter import *
data = ""
list = []
def readText():
mtext=""
mtext = strJson.get()
mlabel2 = Label(myGui,text=mtext).place(x=180,y=200)
data = mtext
def mhello():
_getCurrentOperator(data)
def _getCurrentOperator(data):
if data["type"] == "operation":
_getCurrentOperator(data["rightArgument"])
_getCurrentOperator(data["leftArgument"])
list.append(data["operator"])
elif data["type"] == "group":
_getCurrentOperator(data["argument"])
elif data["type"] == "function":
list.append(data["name"]) # TODO do something with arguments
for i in range(len(data["arguments"])):
_getCurrentOperator(data["arguments"][i])
else:
if (data["value"]) == '':
list.append(data["cell"])
else:
list.append(data["value"])
print(list)
myGui = Tk()
strJson = StringVar()
myGui.title("Simple Gui")
myGui.geometry("400x300")
label = Label(text = 'Welcome!').place(x=170,y=40)
btnStart = Button(myGui,text='Start',command=mhello).place(x=210,y=260)
btnRead = Button(myGui,text='Read text',command=readText).place(x=210,y=200)
txtEntry = Entry(myGui, textvariable=strJson).place(x=150,y=160)
btnOptions = Button(myGui, text = "Options").place(x=150,y=260)
myGui.mainloop()
Bạn có thể cung cấp toàn bộ stacktrace không? –
Xin chào, cảm ơn bạn đã trả lời :) Trong khi chạy chương trình, cửa sổ Call Stack vẫn còn trống –