I'm trying to create a program that will read in a JSON string through the GUI and then use this to perform additional functions, in this case breaking down a mathematical equation. At the moment I am getting the error:
"TypeError: string indices must be integers"
and I have no idea why.
The JSON I am trying to read in is as follows:
{
"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()
You are never parsing the string to a dictionary (json object). Change data = mtext
to: data = json.loads(mtext)
You should also add global data
to the readText method