This article Reprinted from: http://blog.csdn.net/jcodeer/article/details/1811888
'''Tkinter canvas (2 )'''
'''9. Create the tags' of the item '''
#-*-Coding: cp936 -*-
# Use Attribute tags to set the tag of an item
# Use the canvas method gettags to obtain the tags of a specified item
From tkinter import *
Root = TK ()
# Create a canvas and set its background color to white
CV = canvas (root, BG = 'white ')
# Use tags to specify a tag ('r1 ')
RT = cv. create_rectangle (10, 10, 110,110,
Tags = 'r1'
)
Cv. Pack ()
Print cv. gettags (RT)
# Use the tags attribute to specify multiple tags, that is, reset the tags attribute
Cv. itemconfig (RT, tags = ('r2 ', 'r3', 'r4 '))
Print cv. gettags (RT)
Root. mainloop ()
# Dynamically modify the coordinates of an item
'''10. Multiple items use the same tag '''
#-*-Coding: cp936 -*-
# Multiple controls use the same tag
From tkinter import *
Root = TK ()
# Create a canvas and set its background color to white
CV = canvas (root, BG = 'white ')
# Use tags to specify a tag ('r1 ')
RT = cv. create_rectangle (10, 10, 110,110,
Tags = ('r1', 'r2', 'r3 ')
)
Cv. Pack ()
Cv. create_rectangle (20, 20, 80, 80, tags = 'r3 ')
Print cv. find_withtag ('r3 ')
Root. mainloop ()
# Dynamically modify the coordinates of an item
# Fid_withtag returns all items bound to the tag.
'''11. Access item through tag '''
#-*-Coding: cp936 -*-
# This item is obtained when the tag value is obtained. You can set this item.
From tkinter import *
Root = TK ()
# Create a canvas and set its background color to white
CV = canvas (root, BG = 'white ')
# Use tags to specify a tag ('r1 ')
RT = cv. create_rectangle (10, 10, 110,110,
Tags = ('r1', 'r2', 'r3 ')
)
Cv. Pack ()
Cv. create_rectangle (20, 20, 80, 80, tags = 'r3 ')
# Set the color of all items bound to the tag ('r3 ') to blue.
For item in cv. find_withtag ('r3 '):
Cv. itemconfig (item, outline = 'blue ')
Root. mainloop ()
# Dynamically modify the color of the item border bound to the tag ('r3 ')
'''13. Add a tag to another item '''
#-*-Coding: cp936 -*-
# Use addtag _ to add a tag to one or more items
From tkinter import *
Root = TK ()
# Create a canvas and set its background color to white
CV = canvas (root, BG = 'white ')
# Create three rectangles
Rt1 = cv. create_rectangle (
110,110,
Tags = ('r1', 'r2', 'r3 '))
Rt2 = cv. create_rectangle (
20, 20, 80, 80,
Tags = ('s1', 's2', 's3 '))
RT3 = cv. create_rectangle (
30,30, 70,70,
Tags = ('y1 ', 'y2', 'y3 '))
# Add R4 to the previous item of rt2
Cv. addtag_abve ('r4 ', rt2)
# Add R5 to the next item of rt2
Cv. addtag_below ('r5 ', rt2)
For item in [rt1, rt2, rT3]:
Print cv. gettags (item)
Cv. Pack ()
Root. mainloop ()
# Canvas uses the stack technology. The newly created item is always on top of the previous item. Therefore, when you call the above mentioned items, it will find that the item above rt2 is rT3, therefore, the tag ('r4 ') is added to rT3. Similarly, add_below searches for the following item.
'''14. Other items returned '''
#-*-Coding: cp936 -*-
# Use find_xxx to find the previous or next item
From tkinter import *
Root = TK ()
# Create a canvas and set its background color to white
CV = canvas (root, BG = 'white ')
# Create three rectangles
Rt1 = cv. create_rectangle (
110,110,
Tags = ('r1', 'r2', 'r3 '))
Rt2 = cv. create_rectangle (
20, 20, 80, 80,
Tags = ('s1', 's2', 's3 '))
RT3 = cv. create_rectangle (
30,30, 70,70,
Tags = ('y1 ', 'y2', 'y3 '))
# Search for the last item of rt2 and set its border color to red
Cv. itemconfig (cv. find_abve (rt2), outline = 'red ')
# Search for the next item of rt2 and set its border color to green
Cv. itemconfig (cv. find_below (rt2), outline = 'green ')
Cv. Pack ()
Root. mainloop ()
# Canvas uses the stack technology. The newly created item is always on top of the previous item. Therefore, when you call the above mentioned items, it will find that the item above rt2 is rT3, therefore, the border color in rT3 is set to Red. Similarly, add_below searches for the following items.
Canvas in tkinter tutorial (2)