2

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)
falsetru
  • 357,413
  • 63
  • 732
  • 636
xuanzhui
  • 1,300
  • 4
  • 12
  • 30

2 Answers2

1

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)

falsetru
  • 357,413
  • 63
  • 732
  • 636
  • Thanks very much, this does work. I have one more question, does mouse have this kind of issue? I mean whether I need to bind the event after left click is released. If it is, what is the keyword for the bind first parameter? – xuanzhui Dec 25 '14 at 08:37
  • @xuanzhui, I didn't experiment with mouse click. But there are `ButtonPress` and `ButtonRelease` like `KeyPress` and `KeyRelease`. – falsetru Dec 25 '14 at 08:39
0

This has to do with the order that tkinter processes events. The short answer is, custom bindings on widgets are processed before the default bindings, and it's the default bindings that cause the text to be inserted or deleted, the index changed, etc.

See more here: Basic query regarding bindtags in tkinter and How to bind self events in Tkinter Text widget after it will binded by Text widget? and Why Text cursor coordinates are not updated correctly?

Community
  • 1
  • 1
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685