Reputation: 710
I know it is possible to convert coordinate to index in a tkinter Text
widget. But is the opposite also possible?
Basically I want to find out if the mouse coordinate, while the mouse-button is being pressed, is before or after the coordinate of index "10.end"
(for example).
Line number can be anything. As mouse is being pressed and not yet released, I can't use current
.
Upvotes: 1
Views: 542
Reputation: 386010
If you are comparing whether the mouse is over a character before or after another, you don't need to convert an index to a coordinate to do what you want. You can directly compare an index with the x,y coordinates of the event:
def on_motion(event):
event.widget.compare("@%s,%s" % (event.x, event.y), "10.end")
...
text = tk.Text(...)
text.bind("<B1-Motion>", on_motion)
If you are trying to compare if the mouse is to the left or right of another character (regardless of the y coordinate), your only choice is to get the x,y of the character and do the compare yourself.
To do that you can use the bbox
method of the text widget to get the bounding box in pixels of a specific index. You will get back the x/y of the upper left corner of the index, along with the width and the height. You can then compare the x
coordinate of the event with the x
coordinate of the index.
Upvotes: 2