Search code examples
pythontkintertkinter-canvastkinter-text

Getting line mouse is on tkinter text


I am trying to make a sidebar for a tkinter Text widget that displays a red dot next to the line that the mouse hovers over. Currently it is configured to go to the selected line but would be better if it tracked the mouse. I know how to track the mouses y position with root.winfo_pointery() but don't know how the get the corresponding line for that. I also don't want it to display anything if it is out of the text widget's y area or if there is no line. How do I cross compare the y value of the pointer to text lines?

Current code:

from tkinter import *
class BreakpointBar(Canvas):
    def __init__(self, *args, **kwargs):
        #Initializes the canvas
        Canvas.__init__(self, *args, **kwargs, highlightthickness=0)
        self.textwidget = None
        self.ovals = []

    def attach(self, text_widget):
        #Attaches the canvas to the text widget
        self.textwidget = text_widget

    def redraw(self, *args):
        #Redraws the canvas
        """redraw line numbers"""
        # try:
        self.delete("all")
        self.unbind_all("<Enter>")
        self.unbind_all("<Leave>")
        self.ovals = []
        index = self.textwidget.index("insert")
        index_linenum = str(index).split(".")[0]


        i = self.textwidget.index("@0,0")
        print(self.winfo_pointerx())
        print(self.winfo_pointery())
        while True :
            dline= self.textwidget.dlineinfo(i)
            if dline is None: break
            y = dline[1]
            linenum = str(i).split(".")[0]
            if linenum == index_linenum:
                oval = self.create_oval(5, y, 15, y+10, fill="red", outline="red")
                self.tag_bind(oval, "<Enter>", lambda event: self.on_enter(event, oval))
                self.tag_bind(oval, "<Leave>", lambda event: self.on_exit(event, oval))
                self.tag_bind(oval, "<Button-1>", lambda event: self.on_press(event, oval))
                self.ovals.append(oval)
            i = self.textwidget.index("%s+1line" % i)
        # except:
        #     pass

    def on_enter(self, event, oval):
        self.itemconfig(oval, fill="dark red", outline="dark red")


    def on_exit(self, event, oval):
        self.itemconfig(oval, fill="red", outline="red")

    def on_press(self, event, oval):
        index_linenum = int(str(self.textwidget.index("insert")).split(".")[0])
        self.textwidget.insert("{}.end".format(index_linenum), "\nbreakpoint\n")
        self.textwidget.mark_set("insert", "{}.0".format(index_linenum+2))
        self.textwidget.see("insert")
root = Tk()
frame = Frame(root)
frame.pack(expand=True, fill=BOTH)
text=Text(frame)
text.pack(side=RIGHT, fill=BOTH, expand=True)
bb = BreakpointBar(frame, width=20)
bb.attach(text)
bb.pack(side=LEFT, fill=Y)
root.bind("<Button-1>", bb.redraw)
root.bind("<KeyRelease-Return>", bb.redraw)
root.mainloop()

Solution

  • You can get the index for character nearest to the mouse via an x,y coordinate using the format @x,y.

    For example, if you have a function bound to the <Motion> event, you can get the index under the cursor by doing something like this:

    def track_mouse(event):
        index = event.widget.index(f"@{event.x},{event.y}")
        ...
    

    In the above example, index will be in the canonical form of a string in the format line.character.

    It's important to note that you must give coordinates that are relative to the upper-left corner of the text widget itself, not of the window or display as a whole.