title: Getting line mouse is on tkinter text
body: <p>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 <code>root.winfo_pointery()</code> 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?</p>
  <p>Current code:</p>
  <pre><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
          &quot;&quot;&quot;redraw line numbers&quot;&quot;&quot;
          # try:
          self.delete(&quot;all&quot;)
          self.unbind_all(&quot;&lt;Enter&gt;&quot;)
          self.unbind_all(&quot;&lt;Leave&gt;&quot;)
          self.ovals = []
          index = self.textwidget.index(&quot;insert&quot;)
          index_linenum = str(index).split(&quot;.&quot;)[0]
  
  
          i = self.textwidget.index(&quot;@0,0&quot;)
          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(&quot;.&quot;)[0]
              if linenum == index_linenum:
                  oval = self.create_oval(5, y, 15, y+10, fill=&quot;red&quot;, outline=&quot;red&quot;)
                  self.tag_bind(oval, &quot;&lt;Enter&gt;&quot;, lambda event: self.on_enter(event, oval))
                  self.tag_bind(oval, &quot;&lt;Leave&gt;&quot;, lambda event: self.on_exit(event, oval))
                  self.tag_bind(oval, &quot;&lt;Button-1&gt;&quot;, lambda event: self.on_press(event, oval))
                  self.ovals.append(oval)
              i = self.textwidget.index(&quot;%s+1line&quot; % i)
          # except:
          #     pass
  
      def on_enter(self, event, oval):
          self.itemconfig(oval, fill=&quot;dark red&quot;, outline=&quot;dark red&quot;)
  
  
      def on_exit(self, event, oval):
          self.itemconfig(oval, fill=&quot;red&quot;, outline=&quot;red&quot;)
  
      def on_press(self, event, oval):
          index_linenum = int(str(self.textwidget.index(&quot;insert&quot;)).split(&quot;.&quot;)[0])
          self.textwidget.insert(&quot;{}.end&quot;.format(index_linenum), &quot;\nbreakpoint\n&quot;)
          self.textwidget.mark_set(&quot;insert&quot;, &quot;{}.0&quot;.format(index_linenum+2))
          self.textwidget.see(&quot;insert&quot;)
  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(&quot;&lt;Button-1&gt;&quot;, bb.redraw)
  root.bind(&quot;&lt;KeyRelease-Return&gt;&quot;, bb.redraw)
  root.mainloop()
  </code></pre>