Getting Started

« | UserGuide | Coordinate System »

EzGraphics provides easy to use tools for creating and working with simple graphical drawings. Geometric shapes and text are drawn on a canvas that is contained within a top-level or desktop window known as a graphics window. Creating a graphical drawing using EzGraphics requires six steps:

  1. Import the GraphicsWindow class from the ezgraphics module:
    from ezgraphics import GraphicsWindow


  2. Create a graphics window:

    win = GraphicsWindow()

    The new window will automatically be shown on the desktop and contain a canvas that is 400 pixels wide by 400 pixels tall.

    To create a graphics window with a canvas that is of a specific size, you can specify the width and height of the canvas as arguments. Here, we create a graphics window that contains a canvas that is 200 pixels wide by 100 pixels tall.

    win2 = GraphicsWindow(200, 100)
    NoteNote

    When a graphics window is created, the object representing the desktop window is returned and must be stored in a variable as it will be needed in the following steps. In many of our examples, we store the object in a variable named win, but you can use any name.


  3. Give your window a title:

    win.setTitle("My First Drawing")

    Most desktop windows contain text in the title bar that gives the name of the application or the purpose of the window. After creating the graphics window, you can set the window title using the setTitle() method on the graphics window object. If your desktop supports title bars, then the graphics window should look similar to the following


  4. Access the graphics canvas contained in the graphics window:

    canvas = win.canvas()

    To create a drawing, you need some place to draw the geometric shapes and text. In EzGraphics, you draw on a canvas just as an artist would use to create a painting. A graphics canvas is automatically created and placed inside the window when you create the graphics window. In order to draw on the canvas, you must have access to the object representing the canvas within the graphics window.


  5. Create your drawing

    canvas.drawRectangle(40, 40, 100, 200)
    canvas.drawRectangle(200, 200, 150, 50)

    Geometric shapes and text are drawn on the canvas using one of the many “draw” methods defined for use with a canvas by the GraphicsCanvas class. Many of these methods will be described in the following sections. Here, we use the drawRectangle() method to draw two rectangles onto the canvas.


  6. Wait for the user to close the graphics window:

    win.wait()

    After drawing the shapes on the canvas, the program has to stop or pause and wait for the user to close the window (by clicking the close button). Otherwise, the program will immediately terminate and the graphics window would disappear leaving no time for you to see your drawing.


The simple program below combines all of the steps into a single program:

Program: sample.py
## sample.py
#  A simple program that illustrates the use of EzGraphics.
#

from ezgraphics import GraphicsWindow

win = GraphicsWindow()
win.setTitle("My First Drawing")

canvas = win.canvas()
canvas.drawRectangle(40, 40, 100, 200)
canvas.drawRectangle(200, 200, 150, 50)

win.wait()



« | UserGuide | Coordinate System »

Last modified: January 28, 2016, at 07:16 PM.