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

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.

主站蜘蛛池模板: 奎屯市| 漳州市| 平和县| 米脂县| 南投市| 浮山县| 宁海县| 政和县| 西华县| 建阳市| 金昌市| 习水县| 怀远县| 普陀区| 女性| 平潭县| 塔河县| 湟源县| 剑川县| 汕头市| 宿松县| 嘉峪关市| 齐河县| 大兴区| 石渠县| 通城县| 浪卡子县| 和林格尔县| 正安县| 民丰县| 海晏县| 峨边| 郓城县| 仪陇县| 祥云县| 库车县| 崇信县| 上虞市| 建阳市| 蒙城县| 淄博市|