Insertation Cursor (INSERT) trouble with scrolled text in Tkinter

Question:

I’m having a problem with scrolled text in tkinter with the insert cursor.

I’m making a code editor (like notepad but with features), I was making auto bracket close but and it works, but the INSERT cursor which is the line when you are typing (shows where) is one character in front of the closing bracket not inside of it. I could not find a way to move it back,

Entry widgets have the method icursor (insertcursor) but scrolled text does not.

CODE:
st = the scrolled text widget

#bracket open and close

    def bo(event):
      self.st.insert(END, ")")
      
      #code to make insert cursor move back

      return

    
    self.st.bind('<KeyRelease-(>', bo)

Thank you. Have a good day!

Asked By: Chiz

||

Answers:

You can use self.st.mark_set('insert', 'insert-1c') to move the insertion cursor one character back:

def bo(event):
    self.st.insert('insert', ')')
    self.st.mark_set('insert', 'insert-1c')
Answered By: acw1668
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.