Basic HTML Version

|  | Table of Contents |  | View Full Version

Page 135 - 2615_KEC
P. 135

    Let us understand the syntax:

                       point1: Starting position (x1, y1), this is where the line starts. It is defined as a pair of coordinates
                       (x1, y1), which represent the horizontal (x) and vertical (y) distance from the top-left corner of
                       the canvas.
                       point2: Ending position (x2, y2), this is where the line ends. It’s also defined as a pair of coordinates
                       (x2, y2), which are the horizontal and vertical positions from the top-left corner of the canvas.

                       Width: Thickness of the line, this defines how thick the line will be. A higher number means a
                       thicker line.

                       Color: Colour of the line, this is the colour of the line, which you can set to any name or code for
                       a colour (e.g., "red", "blue", "green").

                     Program 1: To draw a Blue Line.

                      Code

                       1   import simplegui
                       2
                       3   # Define the draw function
                       4   def draw(canvas):
                       5   # Draw a blue line from (50, 50) to (200, 200) with a width of 5

                       6       canvas.draw_line((50, 50), (200, 200), 5, "Blue")
                       7
                       8   # Create a frame (canvas) with a size of 300x300
                       9   frame = simplegui.create_frame("Line 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 (this opens the window and starts the drawing)

                       15  frame.start()
                       16

                      Output

                                             Line Example               Expand    Hide    Close

                                              Key:
                                              Mouse:

                                                                                                     Canvas in Python   133

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