Save text from a Tkinter Text object in a variable

Asked

Viewed 926 times

1

I wonder if there’s a way to save the textual content of an object tkinter.Text in a string variable.

Why? Because I believe the Tkinter.Text object is inside a function, and therefore it is not visible to another function that manipulates the text of the Tkinter object.Text.

I know with the method get 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()
  • I didn’t understand your question. Could you exemplify with a code? In other words, if you already know how to get the widget text, what is your question?

1 answer

1

It would not simply be the case to declare textarea in the upper 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()

Browser other questions tagged

You are not signed in. Login or sign up in order to post.