Index

Indexing helps you target a particular place within a piece of text. For example, if you want to mark a particular word in bold, red, or in a different font size, you can do so if you know the index of the starting point and the index of the endpoint that needs to be targeted.

The index must be specified in one of the following formats:

The index format Description
x.y This refers to the character at row x and column y.
@x,y This refers to the character that covers the x,y coordinate within the text's window.
end This refers to the end of the text.
mark This refers to the character after a named mark.
tag.first This refers to the first character in the text that has been tagged with a given tag.
tag.last This refers to the last character in the text that has been tagged with a given tag.
selection (SEL_
FIRST, SEL_LAST)
This corresponds to the current selection. The SEL_FIRST and SEL_LAST constants refer to the start position and end position in the selection. Tkinter raises a TclError exception if there is no selection.
window_name This refers to the position of the embedded window named window_name.
image_name This refers to the position of the embedded image named image_name.
INSERT This refers to the position of the insertion cursor.
CURRENT This refers to the position of the character that is closest to the mouse pointer.

 

Note a small quirk here. The counting of rows in a Text widget starts at 1, while the counting of columns starts at 0. Therefore, the index for the starting position of the Text widget is 1.0 (that is, row number 1 and column number 0).

An index can be further manipulated by using modifiers and submodifiers. Some examples of modifiers and submodifiers are as follows:

  • end - 1 chars or end - 1 c: This refers to the index of the character before the one at the end
  • insert +5lines: This refers to the index five lines ahead of the insertion cursor
  • insertwordstart - 1 c: This refers to the character just before the first one in a word containing the insertion cursor
  • end linestart: This refers to the index of the line start of the end line

Indexes are often used as arguments to functions. Refer to the following list for some examples:

  • my_text.delete(1.0,END) : This means that you can delete from line 1 , column 0 until the end
  • my_text.get(1.0, END) : This gets the content from 1.0 (beginning) to the end
  • my_text.delete('insert-1c', INSERT) : This deletes a character at the insertion cursor
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.218.239.182