Python 2.6 Graphics Cookbook
上QQ阅读APP看书,第一时间看更新

Polygons

Draw a polygon. A polygon is a closed, multi-sided figure. These sides are made up of straight line segments. The specification of points is identical to that of multi-segment lines.

How to do it...

The instructions used in recipe 1 should be used.

Just use the name triangle_polygon.py when you write, save, and execute this program.

# triangle_polygon.py
#>>>>>>>>>>>>>>>>         
from Tkinter import *
root = Tk()
root.title('triangle')

cw = 160                                     # canvas width
ch = 80                                      # canvas height
canvas_1 = Canvas(root, width=cw, height=ch, background="white")
canvas_1.grid(row=0, column=1)                              

# point 1      point 2    point 3
canvas_1.create_polygon(140,30,   130,70,    10,50,   fill="red")
root.mainloop()

How it works...

The results are given in the next screenshot, showing a polygon triangle.

How it works...

The create_polygon() method draws a sequence of straight line segments between the points specified as the arguments of the method. The final point is automatically joined to the first point to close the figure. As the figure is closed you can fill the interior with color.