Reputation: 13061
The value of INSERT will be changed after pressing the BackSpace key, but I got the old position. How can I get the action-after position.
For example:
Th|is is a book.
"|" is the cursor, and the value of INSERT is 1.2
When BackSpace pressed, I got a 1.2 firstly in event handler of , then the cursor moved to 1.1
The same, when I typed This is a book
T => 1.0, h => 1.1, ......,
I think it should be T => 1.1, h => 1.2, ......
There are lot of Keys to change the cursor position, I cannot get the final position to reduce INSERT always by 0.1
Is there any way to get the final position ? or the handler can work after Text widget ?
from tkinter import *
def action(event):
print(event)
position = t.index(INSERT)
print(position)
root = Tk()
t = Text(root)
t.bind('<Key>', action)
t.pack()
root.mainloop()
expect: 1.1 actual result: 1.2
Upvotes: 0
Views: 40
Reputation: 16169
This is because the event '<Key>'
is triggered before the key effect is processed. Events directly bound to the widget always occur before class events, and the edition of the text in the widget is a class binding to the keyboard.
What you want is a binding to '<KeyRelease>'
, which will be triggered after the key is processed:
from tkinter import *
def action(event):
print(event)
position = t.index(INSERT)
print(position)
root = Tk()
t = Text(root)
t.bind('<KeyRelease>', action)
t.pack()
root.mainloop()
Upvotes: 2