The Portable Graphics Library is a simple, object-oriented graphics library designed for students in an introductory programming course. The library has been implemented for a variety of languages and platforms, including C, C++, Java, JavaScript, and Python. Using a common graphics model across a wide variety of platforms makes it easier to translate graphical applications from one language to another, which in turn enables better sharing of assignments and other pedagogical tools.

This page describes the Portable Graphics Library as implemented for Python (Version 0.93). To maintain consistency with Python’s conventions, method names are written using underscores (often called “snake-case”), as in set_color. To maximize portability, the Python implementation accepts the camel-case version of any method name, so that setColor is also acceptable.

The graphics model

The underlying model for the Portable Graphics Library is that of a collage. The GWindow class implements a graphics window that serves as the background plane. Programs then assemble an image on the screen by adding graphical objects—each of which is an instance of one of the GObject subclasses—on top of the GWindow.

The fact that the library uses the collage model means that each new object is placed on top of any existing objects and may obscure them. The order in which objects appear in the window is called the stacking order.

The following code snippet illustrates the graphics model by creating a window and then adding a GRect object that draws a rectangle:

gw = GWindow(500, 300) rect = GRect(150, 50, 200, 100) rect.set_color("Blue") rect.set_filled(True) gw.add(rect)

This program produces the following screen image:

BlueRectangle

The coordinates in the program are measured in pixels, which are the tiny dots that cover the face of the display. The call to GWindow therefore creates a new GWindow object that is 500 pixels wide and 200 pixels high. That object is then assigned to a variable named gw, which makes it possible for the program to refer to the window in the rest of the code.

In keeping with the standard conventions used in computer graphics, the origin of the coordinate system is in the upper left corner, which represents a change from the traditional Cartesian coordinate system. Values for the x coordinate increase as you move from left to right, just as in traditional mathematical usage. Values for the y coordinate, however, increase as you move from top to bottom. For the most part, individual graphical objects use a consistent model in which their position on the screen corresponds to the coordinates of their upper left corner. The GRect object, for example, is created by the line

rect = GRect(150, 50, 200, 100)

which positions the rectangle so that its upper left corner is at the point (150, 50) relative to the upper left corner of the window. The rectangle itself is 200 pixels wide and 100 pixels high. This geometry is illustrated in Figure 1.

Figure 1

The code snippet that creates the image of the rectangle also illustrates the process of setting properties of the GRect object. By default, a GRect object appears only as an outline. The method call

rect.set_filled(True)

changes the properties of the rectangle so that its interior is filled.

Colors in the Portable Graphics Library are specified using strings, which are either a CSS color name (see https://www.w3.org/TR/css-color-4 for a list) or a string in the form "#rrggbb", where rr, gg, and bb, are each two hexadecimal digits giving the red, green, and blue intensities of the color.

The graphical object hierarchy

The available graphics objects form a class hierarchy whose root is the abstract class GObject. The classes in the GObject hierarchy appear in Figure 2.

Figure 1

Class Summary
GArc This class displays an elliptical arc.
GCompound This class defines a graphical object that consists of a collection of other graphical objects.
GDimension This class encapsulates a size containing a width and a height.
GEvent This abstract class forms the root of the event hierarchy.
GFillableObject This abstract class unifies the GObject subclasses that support filling.
GImage This class displays an image.
GKeyEvent This class encapsulates the data associated with a key action.
GLabel This class displays a text string.
GLine This class displays a line segment.
GMouseEvent This class encapsulates the data associated with a mouse action.
GObject This abstract class is the common superclass of all graphical objects.
GOval This class displays an ellipse.
GPoint This class encapsulates a location combining an x and a y coordinate.
GPolygon This class displays a polygon.
GRect This class displays a rectangular box.
GRectangle This class encapsulates the location and dimensions of a rectangle.
GState This class enables clients to pass state information to callback functions.
GTimer This class represents a timer event.
GWindow This class represents the drawing surface to which individual objects are added.