Text#
Text is tkinter’s multi-line text widget (tk.Text), themed by
ttkbootstrap. This page is the complete reference for its own options and
methods — the shared capabilities every widget has (configuration, lifecycle,
focus, introspection) are listed at the bottom.
For a task-oriented walkthrough — indices, tags, marks, search, undo — see the Text widget guide.
Options#
Set at construction or with configure(...); read with cget("name").
Option |
Type |
Description |
|---|---|---|
|
|
Constructor only. |
|
|
The default font for the text. |
|
|
How lines that exceed the width wrap: |
|
|
Tab stops, as a list of distances (optionally each followed by
|
|
|
How tabs interact with the |
|
|
Extra vertical space above the first display line of each text line. |
|
|
Extra vertical space between the display lines of a single wrapped line. |
|
|
Extra vertical space below the last display line of each text line. |
|
|
The text color. |
|
|
The page (background) color. |
|
|
The text color within the selection. |
|
|
The background color of the selection. |
|
|
The selection background when the widget doesn’t have focus (an empty string hides the selection when unfocused). |
|
|
The color of the blinking insert cursor. |
|
|
The width of the insert cursor, in pixels. |
|
|
The 3-D border width drawn around the insert cursor. |
|
|
Milliseconds the cursor is shown per blink. |
|
|
Milliseconds the cursor is hidden per blink ( |
|
|
How the cursor is drawn when the widget lacks focus: |
|
|
|
|
|
The requested width in characters (not pixels). |
|
|
The requested height in lines (not pixels). |
|
|
Internal horizontal padding between the text and the border, in pixels. |
|
|
Internal vertical padding between the text and the border, in pixels. |
|
|
The border style: |
|
|
The width of the 3-D border, in pixels. |
|
|
The border width drawn around selected text. |
|
|
The width of the focus highlight drawn around the widget. |
|
|
The focus-highlight color when the widget has focus. |
|
|
The focus-highlight color when the widget does not have focus. |
|
|
|
|
|
|
|
|
The maximum number of undo steps to keep ( |
|
|
|
|
|
The mouse cursor shown over the widget (see Cursors). |
|
|
Whether the widget accepts focus during keyboard traversal. |
|
|
Whether a selection is exported to the X selection / clipboard. |
|
|
|
|
|
Restrict the widget to start at this line of the underlying store (used by
peer widgets; |
|
|
Restrict the widget to end at this line of the underlying store (used by
peer widgets; |
|
|
A callback connecting the widget to a horizontal scrollbar. |
|
|
A callback connecting the widget to a vertical scrollbar. |
Methods#
Content and editing#
- insert(index, chars, *args)
Insert text at a position. Any trailing
argsalternate a tag (or tuple of tags) with more text, so text can be tagged as it is inserted.- Parameters:
index – where to insert — any index expression (e.g.
"insert","end","1.0").chars (str) – the text to insert.
args – optional alternating tag / text values applied to the inserted runs.
- Returns:
None.
- delete(index1, index2=None)
Delete a character or a range of text.
- Parameters:
index1 – start of the range.
index2 – end of the range, exclusive. If omitted, only the single character at
index1is deleted.
- Returns:
None.
- replace(index1, index2, chars, *args)
Delete the text between two indices and insert
charsin its place — adeletefollowed by aninsertatindex1.- Parameters:
index1 – start of the range to replace.
index2 – end of the range, exclusive.
chars (str) – the replacement text.
args – optional alternating tag / text values, as for
insert.
- Returns:
None.
- get(index1, index2=None)
Return text from the widget.
- Parameters:
index1 – start of the range.
index2 – end of the range, exclusive. If omitted, returns the single character at
index1.
- Returns:
the text in the range.
- Return type:
- count(index1, index2, *options, return_ints=False)
Count text units between two indices.
- Parameters:
index1 – start of the range.
index2 – end of the range.
options – one or more units to count, each a string —
"chars","indices","lines","displaylines","xpixels","ypixels", and so on.return_ints (bool) – return a bare
intwhen a single unit is requested, instead of a one-tuple.
- Returns:
the counts, in the order the options were given.
- Return type:
- dump(index1, index2=None, command=None, **kinds)
Return the contents of a range broken into its pieces — text, tags, marks, and embedded images/windows — as
(key, value, index)triples.- Parameters:
index1 – start of the range.
index2 – end of the range; if omitted, dumps at
index1.command – a callback invoked once per piece instead of returning them.
kinds – booleans selecting which piece types to include —
text,tag,mark,image,window, orall.
- Returns:
a list of
(key, value, index)triples (Noneifcommandis given).- Return type:
Positions and display#
- index(index)
Resolve any index expression to a concrete position.
- Parameters:
index – an index expression (
"insert","end-1c","1.0", …).- Returns:
the position as
"line.column".- Return type:
- compare(index1, op, index2)
Compare two positions.
- see(index)
Scroll the view so that
indexis visible.- Parameters:
index – the position to reveal.
- Returns:
None.
- dlineinfo(index)
Return the bounding box of the display line containing
index.- Parameters:
index – a position on the line.
- Returns:
(x, y, width, height, baseline)in pixels, orNoneif the line is not visible.- Return type:
tuple | None
- bbox(index)
Return the bounding box of the character at
index.- Parameters:
index – the character’s position.
- Returns:
(x, y, width, height)in pixels, orNoneif not visible.- Return type:
tuple | None
- scan_mark(x, y)
Record a starting point for a fast drag-scroll (paired with
scan_dragto).
Marks#
- mark_set(markName, index)
Create a mark, or move an existing one, to a position. A mark is a named position that floats with the text as it changes.
- Parameters:
markName (str) – the mark name (
"insert"is the cursor).index – where to place the mark.
- Returns:
None.
- mark_unset(*markNames)
Delete marks.
- Parameters:
markNames – the marks to remove.
- Returns:
None.
- mark_gravity(markName, direction=None)
Get or set a mark’s gravity — which side it sticks to when text is inserted exactly at it.
- mark_names()
Return all mark names, including the built-in
"insert"and"current".
- mark_next(index)
Return the name of the next mark at or after a position.
- Parameters:
index – where to start.
- Returns:
the mark name, or
""if none.- Return type:
- mark_previous(index)
Return the name of the previous mark before a position (counterpart of
mark_next()).- Return type:
Search#
- search(pattern, index, stopindex=None, *, forwards=None, backwards=None, exact=None, regexp=None, nocase=None, count=None, elide=None)
Search the text for
pattern.- Parameters:
pattern (str) – the text to find, or a Tcl regular expression when
regexp=True.index – where to start searching.
stopindex – where to stop; wraps around the whole widget if omitted.
forwards (bool) – search forward (the default).
backwards (bool) – search backward instead.
exact (bool) – match
patternliterally (the default unlessregexp).regexp (bool) – treat
patternas a regular expression.nocase (bool) – case-insensitive match.
count – an
IntVarthat receives the match length in characters.elide (bool) – also search elided (hidden) text.
- Returns:
the index of the first match, or
""if none.- Return type:
Undo stack#
Each edit_* method wraps the generic edit(*args) dispatcher.
- edit_undo()
Undo the changes back to the last separator. Requires
undo=True.- Returns:
None.- Raises:
TclError – if there is nothing to undo.
- edit_redo()
Redo the changes undone by the last
edit_undo().- Returns:
None.- Raises:
TclError – if there is nothing to redo.
- edit_separator()
Insert a separator (an undo boundary) onto the undo stack.
- Returns:
None.
- edit_reset()
Clear the undo and redo stacks.
- Returns:
None.
Embedded images#
- image_create(index, **options)
Embed an image in the text at a position.
- Parameters:
index – where to insert the image.
options –
image=aPhotoImage,align=("baseline"/"top"/"center"/"bottom"),padx=,pady=,name=.
- Returns:
the name of the embedded image.
- Return type:
- image_cget(index, option)
Return one option of the embedded image at
index.- Return type:
- image_configure(index, **options)
Set (or query) the options of the embedded image at
index.
Embedded windows#
- window_create(index, **options)
Embed a child widget in the text at a position.
- Parameters:
index – where to insert the widget.
options –
window=an existing widget, orcreate=a factory called to build it lazily; plusalign=,padx=,pady=,stretch=.
- Returns:
None.
- window_cget(index, option)
Return one option of the embedded window at
index.
- window_configure(index, **options)
Set (or query) the options of the embedded window at
index.
- window_names()
Return the embedded child widgets.
- Return type:
Scrolling (view)#
- yview(*args)
Query or set the vertical view. Usually connected to a scrollbar via the
yscrollcommandoption rather than called directly.- Returns:
with no args, the visible fraction
(first, last); otherwiseNone.- Return type:
tuple | None
- yview_moveto(fraction)
Scroll vertically so that
fractionof the content is above the top edge.- Parameters:
fraction (float) – a value from 0.0 to 1.0.
- Returns:
None.
- yview_scroll(number, what)
Scroll vertically by a number of units or pages.
- xview(*args)
Horizontal counterpart of
yview();xview_movetoandxview_scrollmirror the vertical versions.
Peer widgets#
- peer_create(newPathName, **options)
Create a second text widget that shares this one’s underlying content — edits to either show in both.
- Parameters:
newPathName (str) – the Tk path name for the new peer.
options – any
Textoptions for the peer.
- Returns:
None.
- peer_names()
Return the peers sharing this widget’s content store.
- Return type:
See also#
Text widget guide — the usage walkthrough.
Tk text manual page — the canonical upstream reference (Tcl 8.6).