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.
Share
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
insertordeletesubcommand of the underlying tk widget command, and the fact that the widget command can be replaced with a Tcl proc.