Basic HTML Version

|  | Table of Contents |  | View Full Version

Page 139 - 2615_KEC
P. 139

    Program 4: To type the Text.

                      Code
                       1   import simplegui

                       2
                       3   # Define the draw function
                       4   def draw(canvas):
                       5   # Draw the text "Hello Python!" at position (80, 150) with font size 30 and colour green
                       6      canvas.draw_text("Hello Python!", (80, 150), 30, "Green")
                       7
                       8   # Create a frame (canvas) with a size of 300x300

                       9   frame = simplegui.create_frame("Text", 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

                      Output
                                             Text                       Expand    Hide    Close

                                                                             Hello Python!

                                              Key:
                                              Mouse:

                     UNDERSTANDING THE FRAME
                     The frame is the main window that holds your canvas where you can draw shapes, lines and text.
                     Without the frame, there would be no visible window to show your work.

                     Every program in SimpleGUI requires you to create a frame, which will display the content you
                     draw on the canvas. The frame defines the size of the window, the title and links the canvas to the

                     program.

                                                                                                     Canvas in Python   137

| 134 135 136 137 138 139 140 141 142 143 144 |