title: Formatting text while typing in a Tkinter Text widget
body: <p>I'm trying to add notes to a given text that's not to be altered. It's pretty much like formatting in common text editors.</p>
  
  <p>I have tried to get the current cursor position via <code>.index('insert')</code>, and then  tag the character either forwards with <code>tag_add(current cursor, current cursor '+1c')</code>, or backwards with <code>tag_add(current cursor + '-1c', current cursor)</code>. This results in  tagging either the character right before or after the character just typed. </p>
  
  <p>Are there any workarounds to live-tag the actually typed character?</p>
  
  <pre><code>import tkinter
  
  
  main = tkinter.Tk()
  
  def typing(event):
      text.tag_configure('note', background='yellow')
      text.tag_configure('note2', background='blue')
      cur_cursor = text.index("insert")
      text.tag_add('note', cur_cursor + '-1c', cur_cursor)
      text.tag_add('note2', cur_cursor, cur_cursor + '+1c')
  
  text = tkinter.Text(main)
  text.grid()
  text.bind('&lt;Key&gt;', typing)
  
  for i in ['OX'*20 + '\n' for i in range(10)]:
      text.insert('end', i)
  
  main.mainloop()
  </code></pre>
  
  <p>Edit: Although Bryan's answer worked for me you might get problems  with fast typing as described here: <a href="https://stackoverflow.com/questions/25798254/python-how-to-get-cursor-position-in-tkinter-text-widget">how-to-get-cursor-position</a></p>