0

How would i go about setting up a Tkinter Text widget to do something similar to IDLE's entry? For example:

>>> Entry goes here!

However, i know how to insert them at the beginning of each line, but how would i go about making it non deletable, so that you cannot delete the >>>? I have searched around on google about this, but to no avail.

Emil Vikström
  • 90,431
  • 16
  • 141
  • 175
IT Ninja
  • 6,174
  • 10
  • 42
  • 65
  • Looks like a dupe of http://stackoverflow.com/questions/7725009/unremovable-text-in-tkinter – gary Jun 24 '12 at 17:20

1 Answers1

1

If the solution only has to be "Good Enough", the technique I would use is this:

  1. When you insert the prompt, remember the index of the end of the prompt.
  2. Add a binding to the widget for the events you care about (eg: <BackSpace> and <Delete> and <<Cut>>)
  3. In this binding you can look at the index of the insertion cursor and selection, and if it's prior to the saved index, ignore the event (ie: do a return "break").

This should work more-or-less OK, though it allows you to insert characters prior to the prompt. Rewriting all the bindings that alter a text widget is a fairly daunting task, but just tracking deletes isn't too hard.

To solve the problem perfectly would require you write a little tcl code to intercept the low level insert and delete commands of the actual widget. It's possible, though it requires a decent understanding of the underlying tcl code. For an example, see this answer: https://stackoverflow.com/a/11180132/7432

Community
  • 1
  • 1
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685