This article Reprinted from: http://blog.csdn.net/jcodeer/article/details/1811347
''' Text (2) in tkinter tutorials '''
'''6. Use tags to specify text attributes '''
# Create a tag with the specified background color
#-*-Coding: cp936 -*-
From tkinter import *
Root = TK ()
T = text (Root)
# Create a tag whose foreground color is red
T. tag_config ('A', foreground = 'red ')
# Use tag 'A' to specify text attributes
T. insert (1.0, '20140901', 'A ')
T. Pack ()
Root. mainloop ()
# The result is that the text color is changed to red.
'''7. Use both texts to specify the same attribute '''
# If there are no special settings, the last one will overwrite all other settings.
#-*-Coding: cp936 -*-
From tkinter import *
Root = TK ()
T = text (Root)
# Create a tag whose foreground color is red
T. tag_config ('A', foreground = 'red ')
T. tag_config ('B', foreground = 'blue ')
# Use tag 'A' to specify text attributes
T. insert (1.0, '123', ('B', 'A '))
T. Pack ()
Root. mainloop ()
# The result is that the text color is not set in the order specified by insert, but in the order of tag creation.
'''8. Control the tag level '''
# Use tag_lower/tag_raise to lower or increase the tag level
#-*-Coding: cp936 -*-
From tkinter import *
Root = TK ()
T = text (Root)
# Create a tag whose foreground color is red
T. tag_config ('A', foreground = 'red ')
T. tag_config ('B', foreground = 'blue ')
# Use tag_lower to lower the level of B
T. tag_lower ('B ')
# Use tag 'A' to specify text attributes
T. insert (1.0, '123', ('B', 'A '))
T. Pack ()
Root. mainloop ()
# Result: the color of the text changes to red, and the blue color is less than red, even if it is first created in red.
'''9. Add a tag to the text block '''
# Use the tag_add Method
#-*-Coding: cp936 -*-
From tkinter import *
Root = TK ()
T = text (Root)
# Create a tag whose foreground color is blue
T. tag_config ('B', foreground = 'blue ')
# Use tag_lower to control the tag level
T. tag_lower ('B ')
# Use tag 'A' to specify text attributes
For I in range (10 ):
T. insert (1.0, '123 ')
T. tag_add ('B', '2. 5', '2. end ')
T. Pack ()
Root. mainloop ()
# Add 10 lines of text to the text, create a tag, and use the tag at the end of 2nd rows and 6th columns to the end of the second row.
'''10. Use a custom mark to add a tag to a text block '''
#-*-Coding: cp936 -*-
# Use the tag_add Method
From tkinter import *
Root = TK ()
T = text (Root)
# Create a tag whose foreground color is blue
T. tag_config ('B', foreground = 'blue ')
# Use tag_lower to control the tag level
T. tag_lower ('B ')
# Use tag 'A' to specify text attributes
For I in range (10 ):
T. insert (1.0, '123 ')
# Customize two marks and use them to specify text blocks for adding tags
T. mark_set ('AB', '3. 1 ')
T. mark_set ('cd', end)
T. tag_add ('B', 'AB', 'cd ')
T. Pack ()
Root. mainloop ()
# Add 10 lines of text to the text, create two marks ('AB' and 'cd'), and use this tag for text blocks specified by the two tags
'''11. Use indexes to obtain the content in text '''
#-*-Coding: cp936 -*-
# Use built-in indexes and custom mark to retrieve text respectively
# Use of the get Method
From tkinter import *
Root = TK ()
T = text (Root)
For I in range (10 ):
T. insert (1.0, '123 ')
# Obtain 1.0-2.3 text
Print T. Get ('1. 0', '2. 3 ')
# Customize two marks and use them to obtain text blocks
T. mark_set ('AB', '3. 1 ')
T. mark_set ('cd', end)
Print T. Get ('AB', 'cd ')
T. Pack ()
Root. mainloop ()
'''12. Test the tag impact of Delete '''
#-*-Coding: cp936 -*-
# The delete method does not affect the tag. That is to say, the delete text has nothing to do with the tag.
From tkinter import *
Root = TK ()
T = text (Root)
# Create a tag whose foreground color is blue
T. tag_config ('B', foreground = 'blue ')
For I in range (10 ):
T. insert (1.0, '123 ')
# Customize two marks and use them to specify text blocks for adding tags
T. mark_set ('AB', '3. 1 ')
T. mark_set ('cd', end)
T. tag_add ('B', 'AB', 'cd ')
# Delete (1.0-4.0) Text
T. Delete ('1. 0', '4. 0 ')
T. Pack ()
Root. mainloop ()
# (1.0-4.0) all the texts are deleted initially, and all the remaining texts are displayed in blue, that is, the tag attributes are retained.
'''13. Influence of tag_delete on text attributes '''
#-*-Coding: cp936 -*-
# Using the tag_delete method to operate tags
From tkinter import *
Root = TK ()
T = text (Root)
# Create a tag whose foreground color is blue
T. tag_config ('B', foreground = 'blue ')
For I in range (10 ):
T. insert (1.0, '123 ')
# Customize two marks and use them to specify text blocks for adding tags
T. mark_set ('AB', '3. 1 ')
T. mark_set ('cd', end)
T. tag_add ('B', 'AB', 'cd ')
# Delete tag 'B'. Note that this operation is performed after tag_add.
T. tag_delete ('B ')
T. Pack ()
Root. mainloop ()
# All texts in the result do not have the tag ('B') attribute, that is, tag_delete clears all attributes related to the tag, whether it is before or after
Text (2) in tkinter tutorial