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

17.7. Methods on Text widgets

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 op holds between index1 and index2. The op specifies what comparison to use, one of: "<", "<=", "==", "!=", ">=", 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 index1. 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 index2. Recall that indices sit between characters.

.get ( index1, index2=None )

Use this method to retrieve the current text from the widget. Retrieval starts at index index1. 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.

.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 option=value pairs.

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 i, this method returns the equivalent position in the form "line.char".

.insert ( index, text, tags=None )

Inserts the given text at the given 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 mark exists, one is created with RIGHT gravity and placed where index points. If the mark already exists, it is moved to the new location.

.mark_unset ( mark )

Removes the named mark.

.search ( pattern, index, option, ... )

Searches for pattern (which can be either a string or a regular expression) in the buffer starting at the given index. If it succeeds, it returns an index of the "line.char" form; if it fails, it returns an empty string.

The allowable options for this method are:

backwardsSet this option to 1 to search backwards from the index. Default is forwards.
countIf 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.
regexpSet this option to 1 to interpret the pattern as a Tcl-style regular expression. The default is to look for an exact match to pattern. Tcl regular expressions are a subset of Python regular expressions, supporting these features: . ^ [c1…] (…) * + ? e1|e2
nocaseSet this option to 1 to ignore case. The default is a case-sensitive search.
stopindexTo 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 tagName with a region of the contents starting just after index index1 and extending up to index index2. If you omit index2, only the character after index1 is tagged.

.tag_bind ( tagName, sequence, func, add=None )

This method binds an event to all the text tagged with tagName. See Section 24, “Events”, below, for more information on event bindings.

To create a new binding for tagged text, use the first three arguments: sequence identifies the event, and func is the function you want it to call when that event happens.

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 sequence arguments.

.tag_cget ( tagName, option )

Use this method to retrieve the value of the given option for the given tagName.

.tag_config ( tagName, option, ... )

To change the value of options for the tag named tagName, pass in one or more option=value pairs.

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:

backgroundThe background color for text with this tag. Note that you can't use bg as an abbreviation.
bgstippleTo 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.
borderwidthWidth of the border around text with this tag. Default is 0. Note that you can't use bd as an abbreviation.
fgstippleTo make the text appear grayish, set this option a bitmap name.
fontThe font used to display text with this tag. See Section 4.4, “Type fonts”.
foregroundThe color used for text with this tag. Note that you can't use the fg abbreviation here.
justifyThe justify option set on the first character of each line determines how that line is justified: LEFT (the default), CENTER, or RIGHT.
lmargin1How 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.
lmargin2How much to indent successive lines of a chunk of text that has this tag. The default is 0.
offsetHow 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”.
overstrikeSet overstrike=1 to draw a horizontal line through the center of text with this tag.
reliefWhich 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”.
rmarginSize of the right margin for chunks of text with this tag. Default is 0.
spacing1This 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.
spacing2This 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.
spacing3This 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.
tabsHow tabs are expanded on lines with this tag. See Section 17.6, “Setting tabs in a Text widget”.
underlineSet underline=1 to underline text with this tag.
wrapHow 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 tagName is moved to a position just below the tag with name belowThis. If you pass only one argument, that tag is moved to the bottom of the tag stack.

.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 tagName starts. The region searched starts at index index1 and ends at index index2. If the index2 argument is omitted, the search goes all the way to the end of the text.

If there is a place in the given region where that tag starts, the method returns a sequence [i0, i1], where i0 is the index of the first tagged character and i1 is the index of the position just after the last tagged character.

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 tagName starts. The region searched starts before index index1 and ends at index index2. If the index2 argument is omitted, the search goes all the way to the end of the text.

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 tagName is moved to a position just above the tag with name aboveThis. If you pass only one argument, that tag is moved to the top of the tag stack.

.tag_ranges ( tagName )

This method finds all the ranges of text in the widget that are tagged with name tagName, and returns a sequence [s0, e0, s1, e1, …], where each si is the index just before the first character of the range and ei is the index just after the last character of the range.

.tag_remove ( tagName, index1, index2=None )

Removes the tag named tagName from all characters between index1 and index2. If index2 is omitted, the tag is removed from the single character after index1.

.tag_unbind ( tagName, sequence, funcid=None )

Remove the event binding for the given sequence from the tag named tagName. If there are multiple handlers for this sequence and tag, you can remove only one handler by passing it as the third argument.

.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 index, pass in one or more option=value pairs.

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:

  1. you can use pass the widget to the window option in this method, or

  2. 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:

alignSpecifies 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.
createA 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.
padxExtra space added to the left and right of the widget within the text line. Default is 0.
padyExtra space added above and below the widget within the text line. Default is 0.
stretchThis 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.
windowThe 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 fraction, where 0.0 moves the text to its leftmost position and 1.0 to its rightmost position.

.xview ( SCROLL, n, what )

The second call moves the text left or right: the what argument specifies how much to move and can be either UNITS or PAGES, and n tells how many characters or pages to move the text to the right relative to its image (or left, if negative).

.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().