Basic HTML Version

|  | Table of Contents |  | View Full Version

Page 136 - 2615_KEC
P. 136

    DRAWING A CIRCLE
                      Drawing a circle involves specifying the centre (middle point), radius (distance from the centre

                      to the edge), width (thickness of the border) and color (colour of the border). This allows you to
                      create circles of different sizes and styles. Here is the syntax to draw a line:

                      canvas.draw_circle(center, radius, width, color)
                      Let us understand the syntax:

                         center: Middle position of the circle, this is the centre point of the circle, specified as (x, y)
                        coordinates. It tells where the circle will be placed on the canvas.
                         radius: Distance from centre to edge, the radius is the distance from the centre of the circle to

                        its edge. A larger radius means a bigger circle.
                          width: Thickness of the border, this defines the thickness of the circle’s border. A larger number

                        means a thicker border.
                         color: Border colour, this defines the colour of the circle’s border (e.g., "red", "blue", "green").

                      Program 2: To draw a Circle.

                       Code

                         1   import simplegui
                        2

                        3    # Define the draw function

                        4    def draw(canvas):
                        5    # Draw a red circle with center (150, 150), radius 80 and border width 4

                        6        canvas.draw_circle((150, 150), 80, 4, "Red")
                        7

                        8    # Create a frame (canvas) with a size of 300x300

                        9    frame = simplegui.create_frame("Circle Example", 300, 300)
                        10

                        11   # Set the draw handler (tells SimpleGUI to use the draw function to display content)

                        12   frame.set_draw_handler(draw)
                        13

                        14   # Start the frame to display the canvas

                        15   frame.start()
                        16

                      134   Premium Edition-VII

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