# Provides a iner that can be used for drawing. It supports basic geometric elements. When using Canvas for drawing, all operations are performed through Canvas, canvas (1) ''' not based on its element ''tkinter )'''
# Provides a Container that can be used for drawing. It supports basic geometric elements. When using Canvas for drawing, all operations are performed through Canvas, not through its elements.
# Handle or tag can be used for element representation.
'''1. The first Canvas program '''
#-*-Coding: cp936 -*-
# Specify the canvas color as white
from Tkinter import *root = Tk()
# Create a Canvas and set its background color to white
cv = Canvas(root,bg = 'white')cv.pack()root.mainloop()
# For the sake of clarity, set the background color to white to distinguish root
'''2. Create an item '''
#-*-Coding: cp936 -*-
# Create a rectangle and specify the color of the canvas as white
From Tkinter import *
Root = Tk ()
# Create a Canvas and set its background color to white
Cv = Canvas (root, bg = 'white ')
# Create a rectangle with coordinates (10, 10, 110,110)
cv.create_rectangle(10,10,110,110)cv.pack()root.mainloop()
# For the sake of clarity, set the background color to white to distinguish root
'''3. Specify the fill color of the item '''
#-*-Coding: cp936 -*-
# Create a rectangle and specify that the background color of the canvas is white.
# Use the property fill to set its fill color
From Tkinter import *
Root = Tk ()
# Create a Canvas and set its background color to white
cv = Canvas(root,bg = 'white')cv.create_rectangle(10,10,110,110,fill = 'red')cv.pack()root.mainloop()
# Specify the filling color of the rectangle to red
'''4. Specify the border color of the item '''
#-*-Coding: cp936 -*-
# Create a rectangle and specify that the background color of the canvas is white.
# Use the attribute outline to set its border color
From Tkinter import *
Root = Tk ()
# Create a Canvas and set its background color to white
cv = Canvas(root,bg = 'white')cv.create_rectangle(10,10,110,110,outline = 'red')cv.pack()root.mainloop()
# The border color of the specified rectangle is red.
'''5. Specify the Border Width '''
#-*-Coding: cp936 -*-
# Specify that the background color of the canvas is white.
# Use the attribute width to specify the line width
From Tkinter import *
Root = Tk ()
# Create a Canvas and set its background color to white
cv = Canvas(root,bg = 'white')cv.create_rectangle(10,10,110,110,outline = 'red',width = 5)cv.pack()root.mainloop()
# Specify the border color of the rectangle as red and set the line width to 5. Note that the width of the rectangle is different from that of the Canvas.
'''6. Draw the dotted line '''
#-*-Coding: cp936 -*-
# Specify that the background color of the canvas is white.
# Use the property dash. The value can only be an odd number.
From Tkinter import *
Root = Tk ()
# Create a Canvas and set its background color to white
cv = Canvas(root,bg = 'white')cv.create_rectangle(10,10,110,110,outline = 'red',dash = 10,fill = 'green')cv.pack()root.mainloop()
# Specify the border color of the rectangle as red and draw a dotted line
'''7. Fill in with a paint brush '''
#-*-Coding: cp936 -*-
# Specify that the background color of the canvas is white.
# Use Attribute stipple
from Tkinter import *root = Tk()
# Create a Canvas and set its background color to white
cv = Canvas(root,bg = 'white')cv.create_rectangle(10,10,110,110,outline = 'red',stipple = 'gray12',fill = 'green')cv.pack()root.mainloop()
# Specify the border color of the rectangle as red and customize the image brush
'''8. Modify the coordinates of items '''
#-*-Coding: cp936 -*-
# Specify that the background color of the canvas is white.
# Use the Canvas method to reset the coordinates of items
from Tkinter import *root = Tk()
# Create a Canvas and set its background color to white
cv = Canvas(root,bg = 'white')rt = cv.create_rectangle(10,10,110,110,outline = 'red',stipple = 'gray12',fill = 'green')cv.pack()
# Reset the coordinates of rt (equivalent to moving an item)
cv.coords(rt,(40,40,80,80))root.mainloop()
# Dynamically modify the coordinates of an item
The above is the content of Canvas (1) in the Tkinter tutorial. For more information, see PHP (www.php1.cn )!