''''Tkinter Tutorial Canvas Chapter (1)'''
# Provides a Container that can be used for drawing and supports basic geometric elements. When using Canvas for drawing, all Operations are all done through Canvas, not through its elements
# Elements can be represented using handle or tag.
'''1. The first Canvas program'''
# -*- coding: cp936 -*-
# 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') cv.pack() root.mainloop()
# For the sake of clarity, set the background color to white to distinguish the root
##'''2. Create an item'''
# -*- coding: cp936 -*-
# Create a rectangle and 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 ')
# Create a rectangle with coordinates (10,10,110,110)
cv.create_rectangle(10,10,110,110) cv.pack() root.mainloop()
# For obviousness, set the background color to white , to distinguish the root
'''3. Specify the fill color of the item'''
# -*- coding: cp936 -*-
# Create a rectangle and specify the background color of the canvas to be white
# Use the attribute 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()
'''4. Specify the border color of the item'''
# -*- coding: cp936 -*-
# Create a rectangle and specify the background color of the canvas to be 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()
# Specify the border color of the rectangle as red
'''5. Specify the width of the border'''
# -*- coding: cp936 -*-
# Specify the background color of the canvas to be white
# Use the attribute width to specify the width of the line
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 is red, set the line width to 5, note that it is different from the width of Canvas.
'''6. Draw a dotted line'''
# -*- coding: cp936 -*-
# Specify canvas The background color is white
# Use attribute dash, this value can only be odd
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 to be red, Draw a dotted line
'''7. Use the brush to fill'''
# -*- coding: cp936 -*-
# Specify the background color of the canvas to be white
# Use the attribute stipple
from Tkinter import * root = Tk()
# to create a Canvas and set its background color Is white
cv = Canvas(root,bg = 'white') cv.create_rectangle(10,10,110,110, outline = 'red', stipple = 'gray12', fill = 'green') cv.pack() root.mainloop()
'''8. Modify the coordinates of the item' ''
# -*- coding: cp936 -*-
# Specify the background color of the canvas as white
# Use Canvas Method to reset the coordinates of the item
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()
cv.coords(rt,(40,40,80,80)) root.mainloop()