官术网_书友最值得收藏!

Indexing and tagging

Though we managed to leverage some built-in functionality to gain a quick advantage, we need more control over the text area so that we can bend it to our will. This will require the ability to target each character or location of the text with precision.

We will need to know the exact position of each character, the cursor, or the selected area in order to do anything with the contents of the editor.

The Text widget offers us the ability to manipulate its content using index, tags, and mark, which let us target a position or place within the text area for manipulation.

Index

Indexing helps you target a particular place within a piece of text. For example, if you want to mark a particular word in bold, red, or in a different font size, you can do so if you know the index of the starting point and the index of the end point that needs to be targeted.

The index must be specified in one of the following formats:

Note a small quirk here. The counting of rows in a Text widget starts at 1, while the counting of columns starts at 0. Therefore, the index for the starting position of the Text widget is 1.0 (that is, row number 1 and column number 0).

An index can be further manipulated by using modifiers and submodifiers. Some examples of modifiers and submodifers are as follows:

  • end - 1 chars or end - 1 c: This refers to the index of the character before the one at the end
  • insert +5lines: This refers to the index of five lines ahead of the insertion cursor
  • insertwordstart - 1 c: This refers to the character just before the first one in a word containing the insertion cursor
  • end linestart: This refers to the index of the line start of the end line

Indexes are often used as arguments to functions. Refer to the following list to have a look at some examples:

  • my_text.delete(1.0,END): This means that you can delete from line 1, column 0 until the end
  • my_text.get(1.0, END): This gets the content from 1.0 (beginning) until the end
  • my_text.delete('insert-1c', INSERT): This deletes a character at the insertion cursor

Tags

Tags are used to annotate text with an identification string that can then be used to manipulate the tagged text. Tkinter has a built-in tag called SEL, which is automatically applied to the selected text. In addition to SEL, you can define your own tags. A text range can be associated with multiple tags, and the same tag can be used for many different text ranges.

Some examples of tagging are as follows:

my_text.tag_add('sel', '1.0', 'end') # add SEL tag from start(1.0) to end
my_text.tag_add('danger', "insert linestart", "insert lineend+1c")
my_text.tag_remove('danger', 1.0, "end")
my_text.tag_config('danger', background=red)
my_text.tag_config('outdated', overstrike=1)

You can specify the visual style for a given tag with tag_config using options such as background(color), bgstipple (bitmap), borderwidth (distance), fgstipple (bitmap), font (font), foreground (color), justify (constant), lmargin1 (distance), lmargin2 (distance), offset (distance), overstrike (flag), relief (constant), rmargin (distance), spacing1 (distance), tabs (string), underline (flag), and wrap (constant).

For a complete reference of text indexing and tagging, type the following command into the Python interactive shell:

>>> import Tkinter
>>> help(Tkinter.Text)

Equipped with a basic understanding of indexing and tagging, let's implement some more features in the code editor.

主站蜘蛛池模板: 拉孜县| 梁河县| 图木舒克市| 蓬安县| 县级市| 临城县| 金湖县| 图木舒克市| 景德镇市| 马尔康县| 东港市| 沂源县| 永嘉县| 和龙市| 天津市| 石柱| 襄樊市| 韶山市| 虹口区| 龙口市| 杭锦后旗| 徐闻县| 庄河市| 怀宁县| 柏乡县| 淮南市| 平罗县| 澄江县| 沙雅县| 镇江市| 修文县| 天长市| 江门市| 繁峙县| 龙胜| 蒙城县| 农安县| 乐清市| 龙井市| 鄂尔多斯市| 桐梓县|