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.

Here are some examples of tagging:

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 about 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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.226.4.191