Search code examples
pythonpython-3.xtkinter

How to get the character before the cursor? Tkinter


Question's in the title. I'm using a text-widget, and I want to get the character that was last typed.

My current idea is to get the character before the cursor, but I can only get the character after the cursor, this is what I have to do that: codeBox.get(codeBox.index(tkinter.CURRENT))

So is there anyway to get the last typed character, or to get the character before the cursor. I only want 1 character, not the whole text widget. Thanks!


Solution

  • Text widget indexex support various modifiers, including the ability to select N characters before or after a given index.

    To get the character immediately before the cursor you need to back up one character from the "insert" index. To do so, use the "-1 characters" or "-1c" modifiers:

    codeBox.get("insert-1c")