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

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.

主站蜘蛛池模板: 本溪市| 商洛市| 嘉义县| 麻江县| 宁津县| 黔江区| 江口县| 雷州市| 富蕴县| 朝阳县| 开封市| 英山县| 长兴县| 富源县| 克拉玛依市| 古田县| 垦利县| 淮北市| 方正县| 洪湖市| 许昌县| 康保县| 临西县| 兴文县| 太和县| 安徽省| 祁东县| 章丘市| 大丰市| 汉沽区| 呈贡县| 三明市| 尖扎县| 大埔县| 淮南市| 麻江县| 寻乌县| 雷山县| 涿鹿县| 文安县| 来宾市|