The Text Widget

Class Text implements a powerful multiline text editor, which can display images and embedded widgets as well as text in one or more fonts and colors. An instance t of Text supports many ways to refer to specific points in t’s contents. t supplies methods and configuration options, which allows fine-grained control of operations, content, and rendering. This section covers a large, frequently used subset of this vast functionality. In some very simple cases, you can get by with just three Text-specific idioms:

t.delete('1.0', END)             # clear the widget's contents
t.insert(END, astring)           # append astring to the widget's contents
somestring = t.get('1.0', END)   # get the widget's contents as a string

END is an index on any Text instance t, indicating the end of t’s text. '1.0' is also an index, indicating the start of t’s text (first line, first column). For more about indices, see Indices.

The ScrolledText Module

The ScrolledText module of Python’s standard library supplies a class named ScrolledText. To construct a ScrolledText instance, call ScrolledText.ScrolledText in exactly the same way you would call Tkinter.Text. A ScrolledText instance s is exactly the same as a Text instance, except that s automatically provides a scrollbar for the Text instance it wraps.

Text Widget Methods

An instance t of Text supplies many methods. (Methods dealing with marks and tags are covered in Marks and Tags.) Many methods accept one or two indices into t’s contents. The most frequently used ...

Get Python in a Nutshell, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.