Nothing is built into tkinter that directly supports this. However, something that is sufficient for most purposes would be to write a function that will query the cursor position and update the highlight at regular intervals.
For instance:
import Tkinter as tk class MyApp(tk.Tk): def __init__(self, *args, **kwargs): tk.Tk.__init__(self, *args, **kwargs) self.text = tk.Text(self) self.text.pack(side="top", fill="both", expand=True) self.text.tag_configure("current_line", background="#e9e9e9") self._highlight_current_line() def _highlight_current_line(self, interval=100): '''Updates the 'current line' highlighting every "interval" milliseconds''' self.text.tag_remove("current_line", 1.0, "end") self.text.tag_add("current_line", "insert linestart", "insert lineend+1c") self.after(interval, self._highlight_current_line) app = MyApp() app.mainloop()
Obviously, the longer the interval, the greater the โlagโ, and the shorter the interval, the more the processor will be used, but there will be a rather large sweet spot between the extremes, where there is almost no noticeable delay and an imperceptible bump in CPU usage.
There is another way to do this, which does not include a survey and is absolutely flawless. You can move the selection exactly when the insertion cursor really moves, but it includes writing some Tcl inline code to create a proxy for the real tk widget, which is hidden in the implementation of the Tkinter Text object.
Finally, the third way is to set up custom bindings for all possible events that change the location of the cursor. Although itโs possible, itโs difficult to get 100% right, because you have to take into account all the events that change the cursor position, as well as process places in the code that can move the cursor without using the event. However, using bindings is a great solution, it requires a bit more work.