Search code examples
pythonpython-3.xinserttkintercursor

Python tkinter Text INSERT CURRENT Cursor


I write a simple program with tkinter Text, and bind arrow down key with a function but the CURRENT and INSERT cursor is not correct when I press the down key.
Firstly, CURRENT sometimes is not updated, and sometimes is updated with wrong index
Secondly, INSERT is always updated, however its index is the last position, for example, if current index is line 1 column 1, then I press Down key, the printed result is still 1.1(line 1 column 1), but my cursor has already come to line 2.
Anyone has any experience on that? Thanks in advance!

def tipKeyDown(event):
    pos=text.index(CURRENT)
    print(pos)
    pos=text.index(INSERT)
    print(pos)

text = Text(textFrm, relief=SOLID)
text.bind('<Button-1>', tipButton1)
text.bind('<Down>', tipKeyDown)

Solution

  • You can use KeyRelease which is raised after the text change.

    text.bind('<KeyRelease-Down>', tipKeyDown)
    

    BTW, CURRENT is corresponds to the character closest to the mouse pointer. (not related to insertion cursor)