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:
You can mix text with different fonts, colors, and backgrounds.
You can intersperse embedded images with text. An image is treated as a single character. See Section 17.3, “Images in text widgets”.
You can even embed in it a “window” containing any Tkinter widget—even a frame widget containing other widgets. A window is also treated as a single character. See Section 17.4, “Windows in text widgets”.
An index is a way of describing a specific position between two characters of a text widget. See Section 17.1, “Indices in text widgets”.
Text widgets allow you to define names for regions of the text called tags. You can change the appearance of a tagged region, changing its font, foreground and background colors, and other attributes. See Section 17.5, “Tags in text widgets”.
A text widget may contain invisible mark objects between character positions. See Section 17.2, “Marks in text widgets”.
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
background | The default background color of the text widget. See Section 4.3, “Colors”. |
bd or
borderwidth | The width of the border around the text widget. Default is 2 pixels. |
cursor | The cursor that will appear when the mouse is over the text widget. See Section 4.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 4.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 23, “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. |
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 4.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=SUNKEN ;
for other values, see Section 4.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=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. |
tabs | This option controls how tab characters
position text. See Section 17.6, “Setting tabs in a Text widget”. |
takefocus | Normally, 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. |
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.
|
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. |