How to slice and access text with TkInter's indices?
Question: In Python, to slice a string to use indices, e.g.

    
    
    word = "Python" # immutable string
    word[2:5]  # characters from position 2 (included) to 5 (excluded)
    

outputs `'tho'`

The Text Widget for TkInter outputs text location at

    
    
    line.char
    

which indicates the _char'th_ character on _line_ line.

If I input a simple text file into a Text Widget GUI as follows:

    
    
    from tkinter import *
    from tkinter.ttk import *
    
    filename = "textfile1.txt"
    
    with open(filename, "rt", encoding='latin1') as in_file:
        readable_file = in_file.read()
    
    root = Tk()
    text = Text(root)
    text.insert(1.0, readable_file)
    text.pack(expand=YES, fill=BOTH)
    root.mainloop()
    

I could access locations in text file. How can I "slice" this string
`readable_file` with the initial `line.char` and the final `line.char`?

Is there a different way to accomplish this?


Answer: Yes, you can simply use the
[`get`](http://effbot.org/tkinterbook/text.htm#Tkinter.Text.get-method) method
of your `Text` widget using the `Text` widget's indices:

    
    
    >>> from tkinter import *
    >>> root = Tk()
    >>> text = Text(root)
    >>> text.insert("end", "Hello world, how are you?")
    >>> text.pack(fill="both", expand=1)
    >>> text.get("1.0", "1.4")
    'Hell'
    >>> text.get("1.4", "end") 
    'o world, how are you?\n'
    >>> text.get("2.0", "end")  # because there's just one line
    ''
    >>> text.get("1.7", "end-1c")  # without the newline at the end
    'orld, how are you?'
    

This is the only way I know to do it easily. You could also get all the text
(again using `Text` widget's indices) and store in in a string, and then
manipulate the string, but it is more work.

