How to retrieve text from a tkinter widget, created with the GUI PAGE generator?

-2

I'm trying to create an interface where the user can type text and save that text in a txt document. To make the process easier, I used a GUI generator called PAGE, which uses the package TKinter, which already comes with Python. The code generated by PAGE was as follows:

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 that I can not recover the text from the window. I tried to use the get () method, but it does not return anything, and I can not call the Text1 object from outside the class, so I can use the solution they gave in this post: link

Any solution?

    
asked by anonymous 11.10.2018 / 14:14

1 answer

0

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

You say that the get() method returns nothing, but that is not possible ... It always returns something , or a string, or error. It's false information that you put in the question.

To get the typed text the command is:

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

Here is a complete example that you can run, I just completed your code with what was missing for it to work in a minimal way:

fromtkinterimportTk,Text,Button,WORD,END,messageboxclassNew_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()
    
11.10.2018 / 15:47