Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7674273
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T16:42:07+00:00 2026-05-31T16:42:07+00:00

I’m working on a simple GUI code editor in Python, and I want to

  • 0

I’m working on a simple GUI code editor in Python, and I want to have the line of text on which the cursor sits to be highlighted at all times.

Right now, my TextEditor class looks like:

class TextEditor:

   def __init__(self, container):
      self.scrollbar = Scrollbar(container)
      self.scrollbar.pack(side=RIGHT, fill=Y)

      self.textbox = Text(container, height=40, undo=True, width=80,
                          font=tkFont.Font(family="Consolas", size=12))
      self.textbox.pack(side=LEFT)

      self.textbox.config(yscrollcommand=self.scrollbar.set)
      self.scrollbar.config(command=self.textbox.yview)

How can I do this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T16:42:08+00:00Added an answer on May 31, 2026 at 4:42 pm

    There is nothing built in to tkinter that directly supports that. However, something that is good enough for most purposes would be to write a function that polls for the cursor position and updates the highlighting at regular intervals.

    For example:

    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.text.tag_raise("sel", "current_line")
    
            self._highlight_current_line()
    
        def _highlight_current_line(self, interval=100):
            '''Updates the 'current line' highlighting every "interval" milliseconds'''
            if not self.text.winfo_exists():
                return
            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, interval)
    
    app = MyApp()
    app.mainloop()
    

    Obviously, the longer the interval the more "lag" that will be introduced, and the shorter the interval the more CPU is used, but there’s a pretty large sweet spot between the extremes where there’s almost no perceptible lag, and an imperceptible bump in CPU usage.

    There’s another way to do it that doesn’t involve polling and is absolutely foolproof. You can move the highlight to precisely when the insertion cursor actually moves, but it involves writing some embedded Tcl code to create a proxy of the actual tk widget that is hidden in the implementation of the Tkinter Text object.

    Finally, a third way is to set up custom bindings for all of the possible events that modify the cursor location. While possible, it’s difficult to get 100% right since you have to account for all events that modify the cursor position, as well as handle places in your code that that might move the cursor without using an event. Still, using bindings is a perfectly good solution, it just requires a bit more work.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a text area in my form which accepts all possible characters from
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a reasonable size flat file database of text documents mostly saved in
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I have a bunch of posts stored in text files formatted in yaml/textile (from
link Im having trouble converting the html entites into html characters, (&# 8217;) i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.