Next / Previous / Contents

24. The Text widget

Text widgets are a much more generalized method for handling multiple lines of text than the Label widget. Text widgets are pretty much a complete text editor in a window:

To create a text widget as the child of a root window or frame named parent:

    w = tk.Text(parent, option, ...)

The constructor returns the new Text widget. Options include:

Table 33. Text widget options

autoseparators If the undo option is set, the autoseparators option controls whether separators are automatically added to the undo stack after each insertion or deletion (if autoseparators=True) or not (if autoseparators=False). For an overview of the undo mechanism, see Section 24.7, “The Text widget undo/redo stack”.
bg or background The default background color of the text widget. See Section 5.3, “Colors”.
bd or borderwidth The width of the border around the text widget; see Section 5.1, “Dimensions”. The default is two pixels.
cursor The cursor that will appear when the mouse is over the text widget. See Section 5.8, “Cursors”.
exportselection Normally, text selected within a text widget is exported to be the selection in the window manager. Set exportselection=0 if you don't want that behavior.
font The default font for text inserted into the widget. Note that you can have multiple fonts in the widgets by using tags to change the properties of some text. See Section 5.4, “Type fonts”.
fg or foreground The color used for text (and bitmaps) within the widget. You can change the color for tagged regions; this option is just the default.
height The height of the widget in lines (not pixels!), measured according to the current font size.
highlightbackground The color of the focus highlight when the text widget does not have focus. See Section 53, “Focus: routing keyboard input”.
highlightcolor The color of the focus highlight when the text widget has the focus.
highlightthickness The thickness of the focus highlight. Default is 1. Set highlightthickness=0 to suppress display of the focus highlight.
insertbackground The color of the insertion cursor. Default is black.
insertborderwidth Size of the 3-D border around the insertion cursor. Default is 0.
insertofftime The number of milliseconds the insertion cursor is off during its blink cycle. Set this option to zero to suppress blinking. Default is 300.
insertontime The number of milliseconds the insertion cursor is on during its blink cycle. Default is 600.
insertwidth Width of the insertion cursor (its height is determined by the tallest item in its line). Default is 2 pixels.
maxundo This option sets the maximum number of operations retained on the undo stack. For an overview of the undo mechanism, see Section 24.7, “The Text widget undo/redo stack”. Set this option to -1 to specify an unlimited number of entries in the undo stack.
padx The size of the internal padding added to the left and right of the text area. Default is one pixel. For possible values, see Section 5.1, “Dimensions”.
pady The size of the internal padding added above and below the text area. Default is one pixel.
relief The 3-D appearance of the text widget. Default is relief=tk.SUNKEN; for other values, see Section 5.6, “Relief styles”.
selectbackground The background color to use displaying selected text.
selectborderwidth The width of the border to use around selected text.
selectforeground The foreground color to use displaying selected text.
spacing1 This option specifies how much extra vertical space is put above each line of text. If a line wraps, this space is added only before the first line it occupies on the display. Default is 0.
spacing2 This option specifies how much extra vertical space to add between displayed lines of text when a logical line wraps. Default is 0.
spacing3 This option specifies how much extra vertical space is added below each line of text. If a line wraps, this space is added only after the last line it occupies on the display. Default is 0.
state Normally, text widgets respond to keyboard and mouse events; set state=tk.NORMAL to get this behavior. If you set state=tk.DISABLED, the text widget will not respond, and you won't be able to modify its contents programmatically either.
tabs This option controls how tab characters position text. See Section 24.6, “Setting tabs in a Text widget”.
takefocus Normally, focus will visit a text widget (see Section 53, “Focus: routing keyboard input”). Set takefocus=0 if you do not want focus in the widget.
undo Set this option to True to enable the undo mechanism, or False to disable it. See Section 24.7, “The Text widget undo/redo stack”.
width The width of the widget in characters (not pixels!), measured according to the current font size.
wrap This option controls the display of lines that are too wide.
  • With the default behavior, wrap=tk.CHAR, any line that gets too long will be broken at any character.

  • Set wrap=tk.WORD and it will break the line after the last word that will fit.

  • If you want to be able to create lines that are too long to fit in the window, set wrap=tk.NONE and provide a horizontal scrollbar.

xscrollcommand To make the text widget horizontally scrollable, set this option to the .set method of the horizontal scrollbar.
yscrollcommand To make the text widget vertically scrollable, set this option to the .set method of the vertical scrollbar.