Next / Previous / Contents / TCC Help System / NM Tech homepage

17. 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 master:

    w = Text ( master, option, ... )

The constructor returns the new text widget. Options include:

bg or backgroundThe default background color of the text widget. See Section 4.3, “Colors”.
bd or borderwidthThe width of the border around the text widget. Default is 2 pixels.
cursorThe cursor that will appear when the mouse is over the text widget. See Section 4.8, “Cursors”.
exportselectionNormally, 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.
fontThe 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 4.4, “Type fonts”.
fg or foregroundThe color used for text (and bitmaps) within the widget. You can change the color for tagged regions; this option is just the default.
heightThe height of the widget in lines (not pixels!), measured according to the current font size.
highlightbackgroundThe color of the focus highlight when the text widget does not have focus. See Section 23, “Focus: routing keyboard input”.
highlightcolorThe color of the focus highlight when the text widget has the focus.
highlightthicknessThe thickness of the focus highlight. Default is 1. Set highlightthickness=0 to suppress display of the focus highlight.
insertbackgroundThe color of the insertion cursor. Default is black.
insertborderwidthSize of the 3-D border around the insertion cursor. Default is 0.
insertofftimeThe number of milliseconds the insertion cursor is off during its blink cycle. Set this option to zero to suppress blinking. Default is 300.
insertontimeThe number of milliseconds the insertion cursor is on during its blink cycle. Default is 600.
insertwidthWidth of the insertion cursor (its height is determined by the tallest item in its line). Default is 2 pixels.
padxThe size of the internal padding added to the left and right of the text area. Default is one pixel. For possible values, see Section 4.1, “Dimensions”.
padyThe size of the internal padding added above and below the text area. Default is one pixel.
reliefThe 3-D appearance of the text widget. Default is relief=SUNKEN; for other values, see Section 4.6, “Relief styles”.
selectbackgroundThe background color to use displaying selected text.
selectborderwidthThe width of the border to use around selected text.
selectforegroundThe foreground color to use displaying selected text.
spacing1This 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.
spacing2This option specifies how much extra vertical space to add between displayed lines of text when a logical line wraps. Default is 0.
spacing3This 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.
stateNormally, text widgets respond to keyboard and mouse events; set state=NORMAL to get this behavior. If you set state=DISABLED, the text widget will not respond, and you won't be able to modify its contents programmatically either.
tabsThis option controls how tab characters position text. See Section 17.6, “Setting tabs in a Text widget”.
takefocusNormally, focus will visit a text widget (see Section 23, “Focus: routing keyboard input”). Set takefocus=0 if you do not want focus in the widget.
widthThe width of the widget in characters (not pixels!), measured according to the current font size.
wrapThis option controls the display of lines that are too wide.
  • With the default behavior, wrap=CHAR, any line that gets too long will be broken at any character.

  • Set wrap=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=NONE and provide a horizontal scrollbar.

xscrollcommandTo make the text widget horizontally scrollable, set this option to the .set method of the horizontal scrollbar.
yscrollcommandTo make the text widget vertically scrollable, set this option to the .set method of the vertical scrollbar.