Reputation: 143
I have an application I'm developing in Tkinter that requires me to get the index of a mouse click inside a tk.Text() widget. How can I do this? Specifically, I am NOT looking for coordinates, but rather the actual character index of the string in the text. I want to then add a char at that index (thus changing the text) and updating the tk.Text() widget.
Upvotes: 1
Views: 1181
Reputation: 3089
As @jasonharper already suggested, you can use text.index("@x,y")
or you can even use text.index("current")
minimal example:
from tkinter import *
def handle(event):
print(text.index(f"@{event.x},{event.y}"), text.index("current"))
root = Tk()
text = Text(root)
text.bind("<Button-1>", handle)
text.pack(expand=True, fill="both")
root.mainloop()
Upvotes: 3