I am doing a program that opens a txt, it loads several fields: IDquestion, Question, Answer, AnswerDate and At. In the txt the fields are separated with a "^". I have an error in a function called "Savevalues", to which I associate a button. What this function intends to do is save the values that the user puts in three text fields corresponding to IDquestion, Question and Answer in a txt aided by two functions: "Entitylist_matrix" and "save", when the button is pressed.
def Savevalues():
IDQuestionlist, Questionlist, Answerlist, AnswerDatelist, Atlist = Entitylist_matrix
IDQuestionlist = IDQuestionlist + [entryIDquestion.get()]
Questionlist = Questionlist + [entryQuestion.get()]
Answerlist = Answerlist + [entryAnswer.get()]
AnswerDatelist = AnswerDatelist + [0] #Le meto un cero por defecto
Atlist = Atlist + [0] #Le pongo un cero por defecto
save
When I run, I get an error in that function that says:
'"function" object is not iterable'
It seems that the problem is in a line that is:
IDQuestionlist, Questionlist, Answerlist, AnswerDatelist, Atlist = Entitylist_matrix
Can not I call a function within another function? o Can I not return variables from another function within a function?
#CARGAMOS LOS DATOS
#Abrimos el archivo
#Pasamos el archivo de txt a formato de listas de entidad.
def txt_Entitylist():
f = open("Questions CE.txt","r")
filechain = f.read()
#Atención! Asegurate que no hay un caracter EOF después del ultimo "0"
#Saco Entitylist
Entitylist = []
Entitylist = filechain.split("|")
f.close()
return Entitylist
#Función para convertir un txt en una matriz
def Entitylist_matrix():
Entitylist = txt_Entitylist()
#Partimos Entitylist en una lista de palabras (wordlist)
wordlist = []
partx = []
for x in Entitylist:
partx = x.split("^")
a = 0
while a < len(partx):
wordlist = wordlist + [partx[a]]
a = a + 1
#IDQuestionlist
IDQuestionlist = []
i = 0
while i<len(wordlist):
IDQuestionlist = IDQuestionlist + [wordlist[i]]
i = i + 5
#Questionlist
Questionlist = []
i = 1
while i<len(wordlist):
Questionlist = Questionlist + [wordlist[i]]
i = i + 5
#Answerlist
Answerlist = []
i = 2
while i<len(wordlist):
Answerlist = Answerlist + [wordlist[i]]
i = i + 5
#AnswerDatelist
AnswerDatelist = []
i = 3
while i<len(wordlist):
AnswerDatelist = AnswerDatelist + [wordlist[i]]
i = i + 5
#Atlist (Delta time)
Atlist = []
i = 4
while i<len(wordlist):
Atlist = Atlist + [wordlist[i]]
i = i + 5
return IDQuestionlist, Questionlist, Answerlist, AnswerDatelist, Atlist
#Función que guarda los datos
def matrix_txt(IDQuestionlist, Questionlist, Answerlist, AnswerDatelist, Atlist):
file=open('Questions CE.txt','w') #Si lo abres en modo "a", añade datos sin borrar los que ya estaban
#1º Quitamos los espacios en blanco que nos sobran de AnswerDatelist y Atlist
#i = 0
#while i<len(Atlist):
# a = str(Atlist[i])
# Atlist[i] = a.strip()
i = 0
while i<len(Atlist):
file.write(""+IDQuestionlist[i]+""+"^")
file.write(""+Questionlist[i]+""+"^")
file.write(""+Answerlist[i]+""+"^")
file.write(" "+str(AnswerDatelist[i]).strip()+" "+"^")
if i + 1 == len(Atlist):
file.write(" "+str(Atlist[i]).strip()+" ")
else:
file.write(" "+str(Atlist[i]).strip()+" "+"^")
i = i + 1
file.close()
#Función para guardar datos desde línea de comandos
def save():
matrix_txt(IDQuestionlist, Questionlist, Answerlist, AnswerDatelist, Atlist)
def Savevalues():
IDQuestionlist, Questionlist, Answerlist, AnswerDatelist, Atlist = Entitylist_matrix
IDQuestionlist = IDQuestionlist + [entryIDquestion.get()]
Questionlist = Questionlist + [entryQuestion.get()]
Answerlist = Answerlist + [entryAnswer.get()]
AnswerDatelist = AnswerDatelist + [0] #Le meto un cero por defecto
Atlist = Atlist + [0] #Le pongo un cero por defecto
save
#Creando una ventanta principal
window=tk.Tk()
window.geometry("500x300+100+100")
window.title("Question Editor")
#Creamos un frame como contenedor
frame = tk.Frame(window)
#Creando un campo de texto para question
entryQuestion=tk.StringVar()
entryQuestion.set("")
txtQuestion=tk.Entry(frame,textvariable=entryQuestion)
txtQuestion.grid(row=0, column=1)
#Creando un campo de texto para answer
entryAnswer=tk.StringVar()
entryAnswer.set("")
txtAnswer=tk.Entry(frame,textvariable=entryAnswer)
txtAnswer.grid(row=1, column=1)
#Creando un campo de texto para IDquestion
entryIDquestion=tk.StringVar()
entryIDquestion.set("")
txtIDquestion=tk.Entry(frame,textvariable=entryIDquestion)
txtIDquestion.grid(row=2,column=1)
#Creando un label para el campo de texto "question"
labelQuestion = tk.Label(frame, text="Question", padx=10 )
labelQuestion.grid(row=0, column=0, sticky=tk.W)
#Creando un label para el campo de texto "answer"
labelAnswer = tk.Label(frame, text="Answer", padx=10 )
labelAnswer.grid(row=1, column=0, sticky=tk.W)
#Creando un label para el campo de texto "IDquestion"
labelIDquestion = tk.Label(frame, text="IDquestion", padx=10)
labelIDquestion.grid(row=2, column=0, sticky=tk.W)
#Definimos un tamaño mínimo de la fila central delgrid para que quede un espacio entre cada entry y posicionamos el frame
frame.grid_rowconfigure(1, minsize=10)
frame.place(x=0,y=140)
#Creando un botón para guardar pregunta y respuesta
btnSave=tk.Button(window,text="Save",command=Savevalues,font=("Agency FB",14))
btnSave.place(x=130,y=210)
#Iniciamos el mailoop
window.mainloop()
#Programa
Entitylist_matrix