Home > Article > Web Front-end > Tkinter Tutorial Canvas Chapter (2)
'''Tkinter Tutorial Canvas Chapter (2)'''
'''9. Create tags for the item'''
# -*- coding: cp936 -*-
# Use the attribute tags to set the tag of the item
# Use the Canvas method gettags to get the tags of the 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 attributes of tags
cv.itemconfig(rt,tags = ('r2','r3','r4')) print cv.gettags(rt) root.mainloop()
# Dynamic Modify the coordinates of the 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 item Coordinates
#fid_withtag returns all items bound to the tag.
'''11. Access item through tag'''
# -*- coding: cp936 -*-
# Once you get the tag value, you get the item, and you can make relevant settings for 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')
# Convert all tags with tag('r3') Set the border color of the bound item to blue
for item in cv.find_withtag('r3'): cv.itemconfig(item,outline = 'blue') root.mainloop()
# Dynamically modify the border color of the item bound to tag('r3')
'''13. Add tags to other items'''
# -*- coding: cp936 -*-
# Use addtag_ to add a or Add tag
from Tkinter import * root = Tk()
# to the next item. Create a Canvas and set its background color to white
cv = Canvas(root,bg = 'white')
# Create three rectangles
rt1 = cv.create_rectangle( 10,10,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_above('r4',rt2)
# Add r4 to rt2 The next item is added r5
cv.addtag_below('r5',rt2) for item in [rt1,rt2,rt3]: print cv.gettags(item) cv.pack() root.mainloop()
#Canvas uses stack technology. The newly created item is always located on top of the previously created item, so when above is called, It will find that the item above rt2 is rt3, so tag('r4') is added to rt3. Similarly, add_below will find the following item.
'''14. Return other items'''
# -*- coding: cp936 -*-
# Use find_xxx 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( 10,10,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'))
# Find the previous item of rt2 and set its border color to red
cv.itemconfig(cv.find_above(rt2),outline = 'red')
# Find 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 stack technology, the newly created The item is always located above the previously created item, so when above is called, it will find the item above rt2 as rt3, so the border color in rt3 is set to red. Similarly, add_below will find the item below.
The above is the content of Canvas Chapter (2) of Tkinter Tutorial. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!