[Tutor] Read - Only Text?

Michael P. Reilly arcege@shore.net
Mon, 5 Mar 2001 07:06:31 -0500 (EST)


> Hi:
> 	I need to set up a widget that :
> 
> 1)Displays text
> 2)Allows copy and paste
> 3)But does "not" allow editing
> 
> Question: I can't find a way to set the TKinter Text widget to
> read - only. That is, not editable.
> 
> Is there a way?
> If not, can someone recommend an alternative?

About the "best" way (which I've still found some problems with) is to
use a Text widget and to unbind the key events in the Text "class".
This is fairly drastic, because it will apply the change to all Text
widgets ("class" here is a GUI term, not Python's classes).  The reason
this has to be done is because Tk has a multilevel event binding: first
the widget's specific binding, then the class's binding.  You could
remove or override the key bindings (<KeyPress>, <KeyRelease>) on the
widget, but the classes bindings would still be there.

How I've removed them is:
  master.unbind_class("Text", "<Return>")
  master.unbind_class("Text", "<Any-KeyPress>")
  master.unbind_class("Text", "<Any-KeyRelease>")
  textwid = Text(master, ...)

Notice that I don't use the widget itself to unbind the Text class.  It
is a global change, so it can work on just about any widget.  After
this you may bind more events to the widget itself.

But, the issue is that this will make the change to ALL Text widgets in
your application, not just the one you create.

You might want to look at my Xmore application where I do this (with
one bug on the ^o key that I never figured out, which does modify the
text).

Good luck,
  -Arcege

<URL: http://www.shore.net/~arcege/python/Xmore.py>

-- 
------------------------------------------------------------------------
| Michael P. Reilly, Release Manager  | Email: arcege@shore.net        |
| Salem, Mass. USA  01970             |                                |
------------------------------------------------------------------------