Tkinter Wiki   ReadOnlyText UserPreferences
 
HelpContents FindPage Diffs Info Edit Subscribe XML Print View

1. Details

A simple, working, does-what-you-expect read-only Text widget. It inherits from the normal Text widget and can be used just like it, the only difference being that the GUI interface will not be able to change the widget's contents.

This first version uses the ?WidgetRedirector class from IDLE, which is included in any recent CPython installation. This may change sometime in the near future.

2. Code

  1 
  2 
  3 
  4 
  5 
  6 
  7 
  8 
  9 
 10 
 11 
    from Tkinter import Text
    from idlelib.WidgetRedirector import WidgetRedirector

    class ReadOnlyText(Text):
        def __init__(self, *args, **kwargs):
            Text.__init__(self, *args, **kwargs)
            self.redirector = WidgetRedirector(self)
            self.insert = \
                self.redirector.register("insert", lambda *args, **kw: "break")
            self.delete = \
                self.redirector.register("delete", lambda *args, **kw: "break")

3. Notes

This is based on information from [WWW]this page in the Tcl/Tk wiki.

There are several possible methods to make a Text widget read-only: ("Amongst our weaponry are such diverse elements as...")



PythonPowered