Next / Previous / Contents

24.1. Text widget indices

An index is a general method of specifying a position in the content of a text widget. An index is a string with one of these forms:

'line.column'

The position just before the given column (counting from zero) on the given line (counting from one). Examples: '1.0' is the position of the beginning of the text; '2.3' is the position before the fourth character of the second line.

'line.end'

The position just before the newline at the end of the given line (counting from one). So, for example, index '10.end' is the position at the end of the tenth line.

tk.INSERT

The position of the insertion cursor in the text widget. This constant is equal to the string 'insert'.

tk.CURRENT

The position of the character closest to the mouse pointer. This constant is equal to the string 'current'.

tk.END

The position after the last character of the text. This constant is equal to the string 'end'.

tk.SEL_FIRST

If some of the text in the widget is currently selection (as by dragging the mouse over it), this is the position before the start of the selection. If you try to use this index and nothing is selected, a tk.TclError exception will be raised. This constant is equal to the string 'sel.first'.

tk.SEL_LAST

The position after the end of the selection, if any. As with SEL_FIRST, you'll get a tk.TclError exception if you use such an index and there is no selection. This constant is equal to the string 'sel.last'.

'markname'

You can use a mark as an index; just pass its name where an index is expected. See Section 24.2, “Text widget marks”.

'tag.first'

The position before the first character of the region tagged with name tag; see Section 24.5, “Text widget tags”.

'tag.last'

The position after the last character of a tagged region.

'@x,y'

The position before the character closest to the coordinate (x, y).

embedded-object

If you have an image or window embedded in the text widget, you can use the PhotoImage, BitmapImage, or embedded widget as an index. See Section 24.3, “Text widget images” and Section 24.4, “Text widget windows”.

In addition to the basic index options above, you can build arbitrary complex expressions by adding any of these suffixes to a basic index or index expression:

+ n chars

From the given index, move forward n characters. This operation will cross line boundaries.

For example, suppose the first line looks like this:

abcdef

The index expression “1.0 + 5 chars” refers to the position between e and f. You can omit blanks and abbreviate keywords in these expressions if the result is unambiguous. This example could be abbreviated “1.0+5c”.

- n chars

Similar to the previous form, but the position moves backwards n characters.

+ n lines

Moves n lines past the given index. Tkinter tries to leave the new position in the same column as it was on the line it left, but if the line at the new position is shorter, the new position will be at the end of the line.

- n lines

Moves n lines before the given index.

linestart

Moves to the position before the first character of the given index. For example, position “current linestart” refers to the beginning of the line closest to the mouse pointer.

lineend

Moves to the position after the last character of the given index. For example, position “sel.last lineend” refers to the end of the line containing the end of the current selection.

wordstart

The position before the beginning of the word containing the given index. For example, index “11.44 wordstart” refers to the position before the word containing position 44 on line 11.

For the purposes of this operation, a word is either a string of consecutive letter, digit, or underbar (_) characters, or a single character that is none of these types.