Indexing is handled in a somewhat coordinate-based way. An index is represented by two numbers separated by a single full stop. For example: 4.5
.
The first number (before the .
) in this index can be thought of as the line number. This begins at 1
.
The second number (after the .
) is how many characters into the line we are. This begins at 0
.
The first character within a Text
widget will therefore be located at 1.0
. This means line 1
, 0
characters in.
To ensure we fully understand this concept, let's create a demo application which will show us where the cursor is located at all times.
Open up a new Python file and enter the following code:
import tkinter as tk win = tk.Tk() current_index = tk.StringVar() text = tk.Text(win, bg="white", fg="black") lab = tk.Label(win, textvar=current_index)
Begin with the normal importing and creation of a main window.
The things we will need for this application are a StringVar
to hold the current cursor location...