'''Tkinter Tutorial Canvas Chapter (3)'''
'''16. Move item'''
# - *- coding: cp936 -*-
# move specifies x, y at the offset
from Tkinter import *
root = Tk()
# Create a Canvas and set its background color to white
cv = Canvas(root,bg = 'white')
# Create two identical rectangles and compare the movements The difference before and after
rt1 = cv.create_rectangle( 10,10,110,110, tags = ('r1','r2','r3')) cv.create_rectangle( 10,10,110,110, tags = ('r1','r2','r3'))
# Move rt1
cv.move(rt1,20,-10) cv.pack() root.mainloop()
# move can specify x, y is a relative offset and can be a negative value
'''17. Delete item'''
# -*- coding: cp936 -*-
# delete deletes the given item
from Tkinter import * root = Tk() cv = Canvas(root,bg = 'white')
# creates two rectangles
rt1 = cv.create_rectangle( 10,10,110,110, tags = ('r1','r2','r3')) r2 = cv.create_rectangle( 20,20,110,110, tags = ('s1','s2','s3'))
# Use id to delete rt1
cv.delete(rt1)
# Use tag to delete r2
cv.delete('s1') cv.pack() root.mainloop()
# Two methods to delete item(id/tag)
'''18. Zoom item'''
# -*- coding: cp936 -*-
# scale scaling item, calculation formula: (coords - offset)*scale + offset
from Tkinter import * root = Tk() cv = Canvas(root,bg = 'white')
# Create two rectangle
rt1 = cv.create_rectangle( 10,10,110,110, tags = ('r1','r2','r3'))
# Enlarge the y coordinate to the original 2 digits, and the x coordinate value remains unchanged
cv.scale(rt1,0,0,1,2) cv.pack() root.mainloop()
# The parameters of scale are (self,xoffset,yoffset,xscale,yscale)
'''19. Binding item and event'''
# -*- coding: cp936 -*-
# Use tag_bind to bind items and events
from Tkinter import * root = Tk()
# Create a Canvas , set its background color to white
cv = Canvas(root,bg = 'white')
# Create three rectangle
rt1 = cv.create_rectangle( 10,10,110,110, width = 8, tags = ('r1','r2','r3')) def printRect(event): print 'rectangle'
# Binding item and events
cv.tag_bind('r1','<Button-1>',printRect) cv.pack() root.mainloop()
# The event will only be triggered when the border of the rectangle is clicked
'''20. Add binding Defined event'''
# -*- coding: cp936 -*-
# Use tag_bind to bind item and event, which is inconsistent with the reference test results.
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, width = 8, tags = ('r1','r2','r3')) def printRect(event): print 'rectangle' def printLine(event): print 'line'
# Bind item and left click event
cv.tag_bind('r1','<Button-1>',printRect)
# Binding item and right-click event
cv.tag_bind('r1','<Button-3>',printLine) cv.pack() root.mainloop()
# The event will only be triggered when the border of the rectangle is clicked. The add parameter is not used. The default is to add to this item. A processing function that does not replace the original event function. Example result: responds to both left and right keys
''' 21. Bind new items to existing tags'''
# -*- coding: cp936 -*-
# Use tag_bind to bind items and events. The test results are inconsistent with the reference statement
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, width = 8, tags = ('r1','r2','r3')) def printRect(event): print 'rectangle' def printLine(event): print 'line'
# Bind item and left click event
cv.tag_bind ('r1','
# Bind item and right-click event
cv.tag_bind('r1','
# Create a line and set its tags to 'r1'
cv.create_line(10,200,100,200,width = 5,tags = 'r1') cv.pack() root.mainloop()
# After binding the event to tag('r1'), create a new item and specify the tag of the bound event. The newly created item is also bound to the event. This is inconsistent with the reference.
The above is the content of Canvas Chapter (3) of Tkinter Tutorial. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!