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 8166145
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T19:45:37+00:00 2026-06-06T19:45:37+00:00

How can I mark a portion of a tkinter text widget as readonly? That

  • 0

How can I mark a portion of a tkinter text widget as readonly? That is, I want to be able to allow editing only in certain parts of the widget. For example, I want to allow editing only after a prompt but not before, to simulate a console.

  • 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-06-06T19:45:40+00:00Added an answer on June 6, 2026 at 7:45 pm

    The most bullet-proof solution is to intercept the low-level insert and delete commands, and put logic in there to prevent insertions and deletions based on some sort of criteria. For example, you could disallow edits within any range of text that has the tag “readonly”.

    Here’s an example of this technique. It takes advantage of the fact that all insertions and deletions ultimately call the insert or delete subcommand of the underlying tk widget command, and the fact that the widget command can be replaced with a Tcl proc.

    try:
        # python 2.x
        import Tkinter as tk
    except ImportError:
        # python 3.x
        import tkinter as tk
    
    class Example(tk.Frame):
        def __init__(self, parent):
            tk.Frame.__init__(self, parent)
    
            text = ReadonlyText(self)
            sb = tk.Scrollbar(self, orient="vertical", command=text.yview)
            text.configure(yscrollcommand=sb.set)
            sb.pack(side="left", fill="y")
            text.pack(side="right", fill="both", expand=True)
    
            text.insert("end", "You can edit this line\n")
            text.insert("end", "You cannot edit or delete this line\n", "readonly")
            text.insert("end", "You can edit this, too.")
    
            text.tag_configure("readonly", foreground="darkgray")
    
    class ReadonlyText(tk.Text):
        '''A text widget that doesn't permit inserts and deletes in regions tagged with "readonly"'''
        def __init__(self, *args, **kwargs):
            tk.Text.__init__(self, *args, **kwargs)
    
            # this code creates a proxy that will intercept
            # each actual insert and delete. 
            self.tk.eval(WIDGET_PROXY)
    
            # this code replaces the low level tk widget 
            # with the proxy
            widget = str(self)
            self.tk.eval('''
                rename {widget} _{widget}
                interp alias {{}} ::{widget} {{}} widget_proxy _{widget} 
            '''.format(widget=widget))
    
    WIDGET_PROXY = '''
    if {[llength [info commands widget_proxy]] == 0} {
        # Tcl code to implement a text widget proxy that disallows
        # insertions and deletions in regions marked with "readonly"
        proc widget_proxy {actual_widget args} {
            set command [lindex $args 0]
            set args [lrange $args 1 end]
            if {$command == "insert"} {
                set index [lindex $args 0]
                if [_is_readonly $actual_widget $index "$index+1c"] {
                    bell
                    return ""
                }
            }
            if {$command == "delete"} {
                foreach {index1 index2} $args {
                    if {[_is_readonly $actual_widget $index1 $index2]} {
                        bell
                        return ""
                    }
                }
            }
            # if we passed the previous checks, allow the command to 
            # run normally
            $actual_widget $command {*}$args
        }
    
        proc _is_readonly {widget index1 index2} {
            # return true if any text in the range between
            # index1 and index2 has the tag "readonly"
            set result false
            if {$index2 eq ""} {set index2 "$index1+1c"}
            # see if "readonly" is applied to any character in the
            # range. There's probably a more efficient way to do this, but
            # this is Good Enough
            for {set index $index1} \
                {[$widget compare $index < $index2]} \
                {set index [$widget index "$index+1c"]} {
                    if {"readonly" in [$widget tag names $index]} {
                        set result true
                        break
                    }
                }
            return $result
        }
    }
    '''
    
    def main():
        root = tk.Tk()
        Example(root).pack(fill="both", expand=True)
        root.mainloop()
    
    if __name__ == "__main__":
        main()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any way that you can mark a form field to be only
In C# you can mark a class as internal so that it is only
In .NET you can mark certain methods as obsolete so that developers are alerted
How can i mark menuitem in Application Bar. I want to get, for example,
I know that you can mark a scala object as @serializable , but I
JBoss 6. I'm led to believe that I can mark a class as @Stateless
I'm working on a website that has a file management portion where users can
I want to create an application where users can mark on map location of
i am currently writing a webapp in rails where users can mark items as
This is solved, but I can't mark it as such for 2 days. This

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.