Basic HTML Version

|  | Table of Contents |  | View Full Version

Page 137 - 2615_KEC
P. 137

    Output

                                             Circle                     Expand    Hide    Close

                                              Key:
                                              Mouse:

                     DRAWING A POLYGON

                     A  polygon  is  a  shape  with  multiple  sides.  In SimpleGUI,  you  can  draw polygons  using  the
                     draw_polygon() function. Here is the syntax to draw a line:

                     canvas.draw_polygon(points, width, color, fill_color)
                     Let us understand the syntax:

                       points: List of corner points, this is a list of coordinates that define the corners of the polygon.
                       Each point is written as (x, y), where x is the horizontal position and y is the vertical position on
                       the canvas.

                       width: Border thickness, this defines how thick or thin the border of the polygon is. A larger
                       number means a thicker border.

                       Color: Border colour, this sets the colour of the polygon’s border. For example, you could choose
                       "black", "red" or any other colour.
                       fill_color: Colour inside the shape, This defines the colour that fills the inside of the polygon. For

                       example, "yellow", "blue", etc.

                     Program 3: To draw a Triangle.

                      Code
                       1   import simplegui

                       2
                       3   # Define the draw function
                       4   def draw(canvas):
                       5       points = [(100, 50), (50, 200), (150, 200)] # The points for the triangle
                               canvas.draw_polygon(points, 3, "Black", "Yellow") # Draw the triangle
                       6
                               with black border and yellow fill
                       7
                       8   # Create a frame (canvas) with a size of 300x300
                       9   frame = simplegui.create_frame("Triangle", 300, 300)

                                                                                                     Canvas in Python   135

| 132 133 134 135 136 137 138 139 140 141 142 |