Home > Web Front-end > H5 Tutorial > Tkinter Tutorial Canvas Chapter (3)

Tkinter Tutorial Canvas Chapter (3)

黄舟
Release: 2017-01-17 10:15:05
Original
1482 people have browsed it


'''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')
Copy after login
Copy after login
Copy after login

# 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'))
Copy after login


# Move rt1


cv.move(rt1,20,-10)
cv.pack()
root.mainloop()
Copy after login


# 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')
Copy after login
Copy after login


# 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'))
Copy after login

# Use id to delete rt1


cv.delete(rt1)


# Use tag to delete r2

cv.delete('s1')
cv.pack()
root.mainloop()
Copy after login


# 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')
Copy after login
Copy after login

# Create two rectangle


rt1 = cv.create_rectangle(
10,10,110,110,
tags = ('r1','r2','r3'))
Copy after login


# 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()
Copy after login


# 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()
Copy after login
Copy after login
Copy after login


# Create a Canvas , set its background color to white


cv = Canvas(root,bg = 'white')
Copy after login
Copy after login
Copy after login

# Create three rectangle


rt1 = cv.create_rectangle(
10,10,110,110,
width = 8,
tags = ('r1','r2','r3'))
def printRect(event):
print 'rectangle'
Copy after login


# Binding item and events


cv.tag_bind(&#39;r1&#39;,&#39;<Button-1>&#39;,printRect)
cv.pack()
root.mainloop()
Copy after login


# 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()
Copy after login
Copy after login
Copy after login


# Create a Canvas and set its background color to white


cv = Canvas(root,bg = &#39;white&#39;)
Copy after login
Copy after login
Copy after login




# Create three rectangles


rt1 = cv.create_rectangle(
10,10,110,110,
width = 8,
tags = (&#39;r1&#39;,&#39;r2&#39;,&#39;r3&#39;))
def printRect(event):
print &#39;rectangle&#39;
def printLine(event):
print 'line'
Copy after login
Copy after login


# Bind item and left click event


cv.tag_bind(&#39;r1&#39;,&#39;<Button-1>&#39;,printRect)
Copy after login

# Binding item and right-click event


cv.tag_bind(&#39;r1&#39;,&#39;<Button-3>&#39;,printLine)
cv.pack()
root.mainloop()
Copy after login

# 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()
Copy after login
Copy after login
Copy after login


# 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 = (&#39;r1&#39;,&#39;r2&#39;,&#39;r3&#39;))
def printRect(event):
print &#39;rectangle&#39;
def printLine(event):
print 'line'
Copy after login
Copy after login


# Bind item and left click event


cv.tag_bind ('r1','',printRect)


# Bind item and right-click event


cv.tag_bind('r1',' ',printLine)


# Create a line and set its tags to 'r1'


cv.create_line(10,200,100,200,width = 5,tags = &#39;r1&#39;)
cv.pack()
root.mainloop()
Copy after login


# 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)!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template