- Tkinter GUI Application Development Blueprints
- Bhaskar Chaudhary
- 557字
- 2021-07-30 10:10:39
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 endinsert +5lines
: This refers to the index of five lines ahead of the insertion cursorinsertwordstart - 1 c
: This refers to the character just before the first one in a word containing the insertion cursorend 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 line1
, column0
until the endmy_text.get(1.0, END)
: This gets the content from1.0
(beginning) until the endmy_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.
- Azure IoT Development Cookbook
- Mastering C# Concurrency
- The HTML and CSS Workshop
- 智能手機APP UI設計與應用任務教程
- INSTANT Silverlight 5 Animation
- Python 3.7從入門到精通(視頻教學版)
- 持續集成與持續交付實戰:用Jenkins、Travis CI和CircleCI構建和發布大規模高質量軟件
- 新印象:解構UI界面設計
- Learning Image Processing with OpenCV
- OpenCV 3.0 Computer Vision with Java
- AI自動化測試:技術原理、平臺搭建與工程實踐
- Android應用程序設計
- C語言程序設計
- R語言:邁向大數據之路
- JavaScript程序設計基礎教程(慕課版)