This article Reprinted from: http://blog.csdn.net/jcodeer/article/details/1811803
'''Tkinter canvas (1 )'''
# 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 (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
Canvas in tkinter tutorial (1)