yassin
yassin

Reputation: 309

tkinter text widget: Setting the insertion curser

I want to create automcomplete feature in a tkinter text widget. When the autocomplete finds a possible word, it deletes the user part-of-word, then insert the complete word:

#if some matched words are found
if self._hits != []:

   #delete the part written by the user
   self.text.delete("%s+1c" % Space1Index,INSERT)

   #Inser the complete word
   self.text.insert("%s+1c" % Space1Index,self._hits[self._hit_index])

Then I will tag the text added by the autocomplete to have a different look than the user input. For ex, if the user wrote te, autocomplete will write the complete word test. te will be with normal font, and st will be wrote in another color and waits for the user to confirm the selected word by the computer.

My question is, after inserting the word test and properly highlighting it, how can I move the INSERT position again after te?

I hope I could clarify my question enough, please let me know if more explanation is needed.

Upvotes: 0

Views: 155

Answers (2)

mgautierfr
mgautierfr

Reputation: 739

You can save the position of the insert mark before your autocompletion changes and reset the mark to the saved position after:

old_pos = self.text.index("insert")
# make autocompletion changes
self.text.mark_set("insert", old_pos)

Upvotes: 1

Bryan Oakley
Bryan Oakley

Reputation: 385900

To move the insertion cursor, set the "insert" mark to wherever you want:

self.text.mark_set("insert", "%s+1c" % ...)

-or-

self.text.mark_set(INSERT, "%s+1c" % ...)

Upvotes: 2

Related Questions