These methods are available on all text widgets:
.compare ( index1
, op
, index2
)
Compares the positions of two indices in the
text widget, and returns true if the relational
holds between
op
and
index1
.
The
index2
specifies what comparison to use, one of:
op
"<"
,
"<="
,
"=="
,
"!="
,
">="
, or
">"
.
For example, for a text widget
t
,
t.compare("2.0", "<=",
END)
returns true if the beginning of
the second line is before or at the end of the text
in t
.
.delete ( index1
, index2
=None )
Deletes text starting just after
.
If the second argument is omitted, only one character
is deleted. If a second index is given, deletion
proceeds up to, but not including, the character
after
index1
.
Recall that indices sit between
characters.index2
.get ( index1
, index2
=None )
Use this method to retrieve the current text
from the widget. Retrieval starts at index
.
If the second argument is omitted, you get the
character after
index1
.
If you provide a second index, you get the text
between those two indices. Embedded images and
windows (widgets) are ignored.index1
.image_cget ( index
, option
)
To retrieve the current value of an option set on an embedded image, call this method with an index pointing to the image and the name of the option.
.image_configure ( index
, option
, ... )
To set one or more options on an embedded
image, call this method with an index pointing to
the image as the first argument, and one or more
pairs.option
=value
If you specify no options, you will get back a dictionary defining all the options on the image, and the corresponding values.
.image_names()
This method returns a tuple of the names of all the text widget's embedded images.
.index ( i
)
For an index
,
this method returns the equivalent position in the
form i
"
.line
.char
"
.insert (
index
,
text
,
tags
=None
)
Inserts the given
at the given
text
.index
If you omit the tags
argument, the newly inserted text will be tagged
with any tags that apply to the characters
both before and after the
insertion point.
If you want to apply one or more tags to the text you are inserting, provide as a third argument a sequence of tag strings. Any tags that apply to existing characters around the insertion point are ignored.
.mark_gravity (
mark
,
gravity
=None
)
Changes or queries the gravity of an existing mark; see Section 17.2, “Marks in text widgets”, above, for an explanation of gravity.
To set the gravity, pass in the name of the
mark, followed by either
LEFT
or
RIGHT
. To find the gravity
of an existing mark, omit the second argument and
the method returns LEFT
or
RIGHT
.
.mark_names()
Returns a sequence of the names of all the
marks in the window, including
INSERT
and
CURRENT
.
.mark_set ( mark
, index
)
If no mark with name
exists, one is created with
mark
RIGHT
gravity and placed
where
points. If the mark already exists, it is moved to
the new location.index
.mark_unset (
mark
)
Removes the named mark.
.search (
pattern
,
index
,
option
,
... )
Searches for
(which can be either a string or a regular
expression) in the buffer starting at the given
pattern
.
If it succeeds, it returns an index of the
index
"
form; if it fails,
it returns an empty string.line
.char
"
The allowable options for this method are:
backwards | Set this option to 1 to search backwards from the index. Default is forwards. |
count | If you set this option to an
IntVar control
variable, when there is a match you can
retrieve the length of the text that
matched by using the
.get() method on
that variable after the method
returns. |
regexp | Set this option to 1 to interpret the
as a
Tcl-style regular expression. The default
is to look for an exact match to
. Tcl
regular expressions are a subset of Python
regular expressions, supporting these
features: . ^ [c1…] (…) * + ?
e1|e2 |
nocase | Set this option to 1 to ignore case. The default is a case-sensitive search. |
stopindex | To limit the search, set this option to the index beyond which the search should not go. |
.see (
index
)
If the text containing the given index is not visible, scroll the text until that text is visible.
.tag_add (
tagName
,
index1
,
index2
=None
)
This method associates the tag named
with a region of the contents starting just after
index
tagName
and extending up to index
index1
.
If you omit
index2
,
only the character after
index2
is tagged.index1
.tag_bind (
tagName
,
sequence
,
func
,
add
=None
)
This method binds an event to all the text
tagged with
.
See Section 24, “Events”, below, for more
information on event bindings.tagName
To create a new binding for tagged text, use
the first three arguments:
identifies the event, and
sequence
is the function you want it to call when that event
happens.func
To add another binding to an existing tag, pass
the same first three arguments and
"+"
as the fourth argument.
To find out what bindings exist for a given sequence on a tag, pass only the first two arguments; the method returns the associated function.
To find all the bindings for a given tag, pass
only the first argument; the method returns a list
of all the tag's
arguments.sequence
.tag_cget (
tagName
,
option
)
Use this method to retrieve the value of the
given
for the given
option
.tagName
.tag_config (
tagName
,
option
,
... )
To change the value of options for the tag
named
,
pass in one or more
tagName
pairs.option
=value
If you pass only one argument, you will get back a dictionary defining all the options and their values currently in force for the named tag.
Here are the options for tag configuration:
background | The background color for text with
this tag. Note that you can't use
bg as an
abbreviation. |
bgstipple | To make the background appear grayish,
set this option to one of the standard
bitmap names (see Section 4.7, “Bitmaps”). This has no effect
unless you also specify a
background . |
borderwidth | Width of the border around text with
this tag. Default is
0 . Note that you
can't use bd as an
abbreviation. |
fgstipple | To make the text appear grayish, set this option a bitmap name. |
font | The font used to display text with this tag. See Section 4.4, “Type fonts”. |
foreground | The color used for text with this tag.
Note that you can't use the
fg abbreviation
here. |
justify | The justify
option set on the first character of each
line determines how that line is justified:
LEFT (the default),
CENTER , or
RIGHT . |
lmargin1 | How much to indent the first line of a
chunk of text that has this tag. The
default is 0 . See
Section 4.1, “Dimensions”for allowable
values. |
lmargin2 | How much to indent successive lines of
a chunk of text that has this tag. The
default is 0 . |
offset | How much to raise (positive values) or lower (negative values) text with this tag relative to the baseline. Use this to get superscripts or subscripts, for example. For allowable values, see Section 4.1, “Dimensions”. |
overstrike | Set
overstrike=1 to draw
a horizontal line through the center of
text with this tag. |
relief | Which 3-D effect to use for text with
this tag. The default is
relief=FLAT ; for
other possible values see Section 4.6, “Relief styles”. |
rmargin | Size
of the right margin for chunks of text with
this tag. Default is
0 . |
spacing1 | This option specifies how much extra
vertical space is put
above each line of text with this tag. 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 with this tag when a logical
line wraps. Default is
0 . |
spacing3 | This option specifies how much extra
vertical space is added below each line of
text with this tag. If a line wraps, this
space is added only after the last line it
occupies on the display. Default is
0 . |
tabs | How tabs are expanded on lines with
this tag. See Section 17.6, “Setting tabs in a Text widget”. |
underline | Set underline=1
to underline text with this tag. |
wrap | How long lines are wrapped in text
with this tag. See the description of the
wrap option for text
widgets, above. |
.tag_delete (
tagName
,
... )
To delete one or more tags, pass their names to this method. Their options and bindings go away, and the tags are removed from all regions of text.
.tag_lower (
tagName
,
belowThis
=None
)
Use this method to change the order of tags in
the tag stack (see Section 17.5, “Tags in text widgets”,
above, for an explanation of the tag stack). If you
pass two arguments, the tag with name
is moved to a position just below the tag with name
tagName
.
If you pass only one argument, that tag is moved to
the bottom of the tag stack.belowThis
.tag_names (
index
=None
)
If you pass an index argument, this method returns a sequence of all the tag names that are associated with the character after that index. If you pass no argument, you get a sequence of all the tag names defined in the text widget.
.tag_nextrange (
tagName
,
index1
,
index2
=None
)
This method searches a given region for places
where a tag named
starts. The region searched starts at index
tagName
and ends at index
index1
.
If the
index2
argument is omitted, the search goes all the way to
the end of the text.index2
If there is a place in the given region where
that tag starts, the method returns a sequence
[
, where
i0
,
i1]
is the index of the first tagged character and
i0
is the index of the position just after the last
tagged character.i1
If no tag starts are found in the region, the method returns an empty string.
.tag_prevrange (
tagName
,
index1
,
index2
=None
)
This method searches a given region for places
where a tag named
starts. The region searched starts
before index
tagName
and ends at index
index1
.
If the
index2
argument is omitted, the search goes all the way to
the end of the text.index2
The return values are as in
.tag_nextrange()
.
.tag_raise (
tagName
,
aboveThis
=None
)
Use this method to change the order of tags in
the tag stack (see Section 17.5, “Tags in text widgets”,
above, for an explanation of the tag stack). If
you pass two arguments, the tag with name
is moved to a position just above the tag with name
tagName
.
If you pass only one argument, that tag is moved to
the top of the tag stack.aboveThis
.tag_ranges (
tagName
)
This method finds all the ranges of text in the
widget that are tagged with name
,
and returns a sequence
tagName
[
, where each
s0
,
e0
,
s1
,
e1
,
…]
is the index just
before the first character of the range and
si
is the index just after the last character of the
range.ei
.tag_remove (
tagName
,
index1
,
index2
=None
)
Removes the tag named
from all characters between
tagName
and
index1
.
If
index2
is omitted, the tag is removed from the single
character after
index2
.index1
.tag_unbind (
tagName
,
sequence
,
funcid
=None
)
Remove the event binding for the given
from the tag named
sequence
.
If there are multiple handlers for this sequence
and tag, you can remove only one handler by passing
it as the third argument.tagName
.window_cget (
index
,
option
)
Returns the value of the given
option
for the embedded
widget at the given
index.
.window_configure (
index
,
option
)
To change the value of options for embedded
widget at the given
,
pass in one or more
index
pairs.option
=value
If you pass only one argument, you will get back a dictionary defining all the options and their values currently in force for the given widget.
.window_create (
index
,
option
,
... )
This method creates a window where a widget can be embedded within a text widget. There are two ways to provide the embedded widget:
you can use pass the widget to the
window
option in this method,
or
you can define a procedure that will create
the widget and pass that procedure as a
callback to the create
option.
Options for
.window_create()
are:
align | Specifies how to position the embedded
widget vertically in its line, if it isn't
as tall as the text on the line. Values
include:
align=CENTER (the
default), which centers the widget
vertically within the line;
align=TOP , which
places the top of the image at the top of
the line;
align=BOTTOM , which
places the bottom of the image at the
bottom of the line; and
align=BASELINE ,
which aligns the bottom of the image with
the text baseline. |
create | A procedure that will create the embedded widget on demand. This procedure takes no arguments and must create the widget as a child of the text widget and return the widget as its result. |
padx | Extra space added to
the left and right of the widget within the
text line. Default is
0 . |
pady | Extra space added above and below the
widget within the text line. Default is
0 . |
stretch | This option controls what happens when
the line is higher than the embedded
widget. Normally this option is
0 , meaning that the
embedded widget is left at its natural
size. If you set
stretch=1 , the
widget is stretched vertically to fill the
height of the line, and the
align option is
ignored. |
window | The widget to be embedded. This widget must be a child of the text widget. |
.window_names()
Returns a sequence containing the names of all embedded widgets.
.xview ( MOVETO,
fraction
)
This method scrolls the text widget
horizontally, and is intended for binding to the
command option of a related
horizontal scrollbar.
This method can be called in two different
ways. The first call positions the text at a value
given by
,
where 0.0 moves the text to its leftmost position
and 1.0 to its rightmost position.fraction
.xview ( SCROLL,
n
,
what
)
The second call moves the text left or right:
the
argument specifies how much to move and can be
either what
UNITS
or
PAGES
, and
tells how many characters or pages to move the text
to the right relative to its image (or left, if
negative).n
.xview_moveto (
fraction
)
This method scrolls the text in the same way as
.xview(MOVETO,
.fraction
)
.xview_scroll (
n
,
what
)
Same as .xview(SCROLL,
.
n
,
what
)
.yview(MOVETO,
fraction
)
The vertical scrolling equivalent of
.xview(MOVETO,…)
.
.yview(SCROLL,
n
,
what
)
The vertical scrolling equivalent of
.xview(SCROLL,…)
.
When scrolling vertically by
UNITS
, the units are lines.
.yview_moveto(fraction
)
The vertical scrolling equivalent of
.xview_moveto()
.
.yview_scroll(n
,
what
)
The vertical scrolling equivalent of
.xview_scroll()
.