Hung Truong Hung Truong - 4 months ago 33
Python Question

Tkinter Text widget : Fetching Cursor Selection

I have a text widget, and I have the text

this is some random text


With the keyboard cursor in the word "
random
". How can I get the start and end index of the cursor selection.

Thanks in advance!

Answer

You can get the index of the start of the word by applying the modifier "wordstart" to an index. To get the end of the word, use "wordend".

For example:

start = text.index("insert wordstart")
end = text.index("insert wordend")