Save text of a tkinter Text object to a variable

1

I would like to know if there is a way to save the textual content of a tkinter.Text object to a string variable.

Why? Because I create the tkinter.Text object within a function, and therefore the other function that manipulates the text of the tkinter.Text object is not visible.

I know that with the get method I can retrieve the text of the object in the scope of the latter.

def manipulateText():
    # Gostaria de manipular o texto do objecto 'textArea',
    # mas textArea não é visível aqui.
    # Se textArea fosse do tipo 'Entry'
    # eu poderia salvar o texto numa variável do tipo tkinter.StringVar()
    # mas tkinter.Text não fornece esta possibilidade.

def func():
    win = tkinter.Tk()
    textArea = tkinter.Text(win)
    textArea.pack()
    win.mainloop()
    
asked by anonymous 08.10.2014 / 18:42

1 answer

2

Would not it be just the case of declaring textarea at the top scope? (i.e., in the class or module)

textarea = None

def manipulateText():
    # Lê o texto
    texto = textarea.get(1.0, END)
    # Insere mais texto
    textarea.insert(END, "hello, world")
    # Etc

def func():
    win = tkinter.Tk()
    textArea = tkinter.Text(win)
    textArea.pack()
    win.mainloop()
    
08.10.2014 / 23:09