How to recover text from a Tkinter widget created with the Guis PAGE generator?

Asked

Viewed 151 times

-2

I am trying to create an interface where the user can type a text and save that text in a txt document. To make the process easier, I used a Guis generator called PAGE, which uses the Tkinter package, which already comes with Python. The code that PAGE generated was the following:

class New_Toplevel:
    def __init__(self, top=None): 
        self.Text1 = Text(top)
        self.Text1.place(relx=0.017, rely=0.133, relheight=0.831, 
                              relwidth=0.94)
        self.Text1.configure(background="white")
        self.Text1.configure(font="TkTextFont")
        self.Text1.configure(foreground="black")
        self.Text1.configure(highlightbackground="#d9d9d9")
        self.Text1.configure(highlightcolor="black")
        self.Text1.configure(insertbackground="black")
        self.Text1.configure(selectbackground="#c4c4c4")
        self.Text1.configure(selectforeground="black")
        self.Text1.configure(width=564)
        self.Text1.configure(wrap=WORD)

The problem is I can’t recover the text from the window. I tried to use the get() method, but it does not return anything, and I can’t call the Text1 object from outside the class, so I can use the solution they gave in this post: https://stackoverflow.com/questions/14824163/how-to-get-the-input-from-the-tkinter-text-box-widget

Some solution?

1 answer

0


Your question is incomplete, failed to put the code where you try to access the self.Text1 and the error that is generating.

You say the method get() does not return anything, but that is not possible... He always returns something, or a string, or an error. It is a false information that you put in the question.

To get the typed text the command is:

texto_digitado = self.Text1.get(1.0, END)

Below is a complete example that you can run, I only completed your code with what was missing for it to work in a minimum:

imagem da tela

from tkinter import Tk, Text, Button, WORD, END, messagebox


class New_Toplevel:
    def __init__(self, top=None): 
        Button(top, text='OK', command=self.clicou).pack()
        self.Text1 = Text(top)
        self.Text1.place(relx=0.017, rely=0.133, relheight=0.831, 
                              relwidth=0.94)
        self.Text1.configure(background="white")
        self.Text1.configure(font="TkTextFont")
        self.Text1.configure(foreground="black")
        self.Text1.configure(highlightbackground="#d9d9d9")
        self.Text1.configure(highlightcolor="black")
        self.Text1.configure(insertbackground="black")
        self.Text1.configure(selectbackground="#c4c4c4")
        self.Text1.configure(selectforeground="black")
        self.Text1.configure(width=564)
        self.Text1.configure(wrap=WORD)

    def clicou(self):
        texto = self.Text1.get(1.0, END)
        with open('arquivo.txt', 'w') as f:
            f.write(texto)
        messagebox.showinfo(title='Conteúdo', message='O texto contém: ' + texto)

root = Tk()
root.geometry('500x500')    
t = New_Toplevel(root)
root.mainloop()

Browser other questions tagged

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