Skip to content Skip to sidebar Skip to footer

Get Position In Tkinter Text Widget

I'm trying to find a reliable way of getting the current cursor position in a tkinter text widget. What I have so far is: import tkinter as tk def check_pos(event): print(t.i

Solution 1:

Thanks to Bryan Oakley for pointing me in the right direction with the links he posted in the comments. I chose the third choice which introduces an additional binding. The working code is below. Now the binding happens after the class is bound so that the change of position in the Text widget is visible to the function.

import tkinter as tk

def check_pos(event):
    print(t.index(tk.INSERT))

root = tk.Tk()

t = tk.Text(root)
t.pack()
t.bindtags(('Text','post-class-bindings', '.', 'all'))

t.bind_class("post-class-bindings", "<KeyPress>", check_pos)
t.bind_class("post-class-bindings", "<Button-1>", check_pos)


root.mainloop()

Post a Comment for "Get Position In Tkinter Text Widget"