This article Reprinted from: http://blog.csdn.net/jcodeer/article/details/1811348
''Tkinter text (3 )'''
'''14. Two built-in attributes of the custom tag '''''
# Tag. First: insert text before the tag. This text is not included in this tag
# Tag. Last: insert text after tag. This text is included in this tag
#-*-Coding: cp936 -*-
# Use the built-in attributes of tags to insert text
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.
# Insert 'first 'before the tag (' B'
T. insert ('B. First', 'First ')
# Insert 'last' after tag ('B'
T. insert ('B. last', 'last ')
T. Pack ()
Root. mainloop ()
# Note: first does not use the tag ('B') attribute, and last uses the tag ('B') attribute.
'''15. Create a button in text '''
#-*-Coding: cp936 -*-
# Use window_create to create a widget in text
From tkinter import *
Root = TK ()
T = text (Root)
For I in range (10 ):
T. insert (1.0, '123 ')
Def printtext ():
Print 'buttin in text'
Bt = button (T, text = 'button ', command = printtext)
# Create a button in text
T. window_create ('2. 0', window = BT)
# Pack () not called ()
# Bt. Pack ()
T. Pack ()
Root. mainloop ()
# Note: Use window_create instead of insert ('2. 0', BT); Pack () does not need to be called;
# Click this button to print the 'button in text', which proves that this button works properly.
'''16. Create an image in text (not implemented )'''
#-*-Coding: cp936 -*-
# Use window_create to create a widget in text
From tkinter import *
Root = TK ()
T = text (Root)
For I in range (10 ):
T. insert (1.0, '123 ')
# Bitmapimage and photoimage are used for testing respectively. No images are displayed ???
# Bm = bitmapimage ('gray75 ')
Bm = photoimage ('C:/python.gif ')
# Create an image in text
T. image_create ('2. 0', image = BM)
Print T. image_names ()
# Printed image names are correct
T. Pack ()
Root. mainloop ()
# I do not know why this effect is not achieved according to the instructions in the manual.
'''17. Bind tags and events '''
#-*-Coding: cp936 -*-
# Use the tag_bind Method
From tkinter import *
Root = TK ()
T = text (Root)
For I in range (10 ):
T. insert (1.0, '123 ')
# Create a tag
T. tag_config ('A', foreground = 'blue', underline = 1)
# Enter callback function
Def entertag (event ):
Print 'enter event'
# Binding tags ('A') and events ('<enter> ')
T. tag_bind ('A', '<enter>', entertag)
T. insert (2.0, 'enter event', 'A ')
T. Pack ()
Root. mainloop ()
# Note: Use tag_bind to bind tags and events. When this event occurs on a tag, the tag callback function is called.
# Because the enter event is used, this event contains a parameter. Therefore, an entertag parameter is added. This parameter is not used in the program.
'''18. Use edit_xxx to implement common editing functions (not implemented )'''
#-*-Coding: cp936 -*-
# Use the edit_xxx function to implement common editing functions
From tkinter import *
Root = TK ()
T = text (Root)
For I in range (10 ):
T. insert (1.0, '123 ')
T. Pack ()
# Define callback Functions
# Undo callback function
Def undotext ():
T. edit_undo ()
# Insert text functions
Def inserttext ():
T. insert (1.0, 'insert text ')
Button (root, text = 'undo ', command = undotext). Pack (fill = X)
Button (root, text = 'insert text', command = inserttext). Pack (fill = X)
Root. mainloop ()
# This edit_undo method does not work either. Why ???
Text in tkinter tutorial (3)