Python – How to get the input from the Tkinter Text Widget

pythontkinter

How to get Tkinter input from the Text widget?

EDIT

I asked this question to help others with the same problem – that is the reason why there is no example code. This issue had been troubling me for hours and I used this question to teach others. Please do not rate it as if it was a real question – the answer is the thing that matters.

Best Answer

To get Tkinter input from the text box, you must add a few more attributes to the normal .get() function. If we have a text box myText_Box, then this is the method for retrieving its input.

def retrieve_input():
    input = self.myText_Box.get("1.0",END)

The first part, "1.0" means that the input should be read from line one, character zero (ie: the very first character). END is an imported constant which is set to the string "end". The END part means to read until the end of the text box is reached. The only issue with this is that it actually adds a newline to our input. So, in order to fix it we should change END to end-1c(Thanks Bryan Oakley) The -1c deletes 1 character, while -2c would mean delete two characters, and so on.

def retrieve_input():
    input = self.myText_Box.get("1.0",'end-1c')