Tkinter Text’s index
The index of a Text object is not a single number, but a string. The purpose of indexing is to allow the Text control to handle further file operations. The following are common forms of indexing.
(1) line/column ("line.column"): the counting method line starts from 1, column starts from 0, separated by a period.
(2) INSERT: the current position of the insertion point.
(3) CURRENT: the current position of the cursor relative to the character.
(4) END: the position after the last character in the buffer.
(5) Expression: The index uses the expression.
+count chars
, count is a number, e.g., "+2c" moves the index back two characters.-count chars
, count is a number, e.g., "-2c" moves the index forward by two characters.
The above is expressed as a string, but you can also use the index( ) method to actually list the index contents as a string.
Example#1
The selected text is also displayed as the usual "line.column" string.
from tkinter import *
def selectedText():
try:
selText = text.get(SEL_FIRST,SEL_LAST)
print("Select text:",selText)
# print("SEL_FIRST:",type(SEL_FIRST)," SEL_LAST:",SEL_LAST)
print("selectionStart:",text.index(SEL_FIRST))
print("selectionEnd:",text.index(SEL_LAST))
except TclError:
print("No text selected")
root = Tk()
root.title("apidemos.com")
root.geometry("300x180")
# create Button
btn = Button(root,text="Print selection",command=selectedText)
btn.pack(pady=3)
# create Text
text = Text(root)
text.pack(fill=BOTH,expand=True,padx=3,pady=2)
text.insert(END,"Love You Like A Love Song") # insert text
root.mainloop()
Output:
Example#2
Lists the index of INSERT, CURRENT, and END.
from tkinter import *
def printIndex():
print("INSERT :",text.index(INSERT))
print("CURRENT:",text.index(CURRENT))
print("END :",text.index(END))
print("******************************")
root = Tk()
root.title("apidemos.com")
root.geometry("300x180")
# create Button
btn = Button(root,text="Print index",command=printIndex)
btn.pack(pady=3)
#create Text
text = Text(root)
text.pack(fill=BOTH,expand=True,padx=3,pady=2)
text.insert(END,"Love You Like A Love Song\n") # insert text
text.insert(END,"apidemos.com") # insert text
root.mainloop()
Output:
Since the mouse cursor has been on the Print index button, the CURRENT listed is at the 1.0 index location, in fact if we click on the file location, the CURRENT index location will change, at this time the INSERT index location will change with the CURRENT. Previously we understood that using the insert( ) method, you can insert text at the end of the file, when we understand the concept of index, we can actually use the index position to insert the file as well.
Example#3
Inserts text at the specified index position.
from tkinter import *
root = Tk()
root.title("apidemos.com")
root.geometry("300x180")
# create Text
text = Text(root)
text.pack(fill=BOTH,expand=True,padx=3,pady=2)
text.insert(END,"Love You Like A Love Song\n") # insert text
text.insert(1.14,"apidemos.com") # insert text
root.mainloop()
Output:
The above program focuses on inserting "apidemos.com" at line=1, column=14.