campy.graphics.gwindow module

Provide a GWindow that supports drawing graphical objects on screen.

class campy.graphics.gwindow.Alignment[source]

Bases: enum.Enum

Horizontal alignment within a region.

CENTER = 1
LEFT = 0
RIGHT = 2
class campy.graphics.gwindow.CloseOperation[source]

Bases: enum.Enum

Actions to take upon closure of a GWindow.

Currently unused.

DISPOSE = 2
DO_NOTHING = 0
EXIT = 3
HIDE = 1
class campy.graphics.gwindow.GWindow(width=500, height=500, visible=True, title='', color=None, top=None)[source]

Bases: object

A Graphics Window that supports simple graphics.

Each GWindow consists of two layers. The background layer provides a surface for drawing static pictures that involve no animation. Graphical objects drawn in the background layer are persistent and do not require the client to update the contents of the window. The foreground layer contains graphical objects that are redrawn as necessary.

The GWindow class includes several methods that draw lines, rectangles, and ovals on the background layer without making use of the facilities of the gobjects.h interface. For example, the following program draws a diamond, rectangle, and oval at the center of the window:

gw = gwindow.GWindow
print("This program draws a diamond, rectangle, and oval.")
width = gw.getWidth()
height = gw.getHeight()
gw.drawLine(0, height / 2, width / 2, 0);
gw.drawLine(width / 2, 0, width, height / 2);
gw.drawLine(width, height / 2, width / 2, height);
gw.drawLine(width / 2, height, 0, height / 2);
gw.setColor("BLUE");
gw.fillRect(width / 4, height / 4, width / 2, height / 2);
gw.setColor("GRAY");
gw.fillOval(width / 4, height / 4, width / 2, height / 2);
DEFAULT_HEIGHT = 500
DEFAULT_WIDTH = 500
add(gobj, x=None, y=None)[source]

Add a GObject to the foreground layer of this GWindow.

If both x and y are supplied, the object is moved to that location before adding.

To add a GOval to the window:

window = GWindow()
oval = GOval(0, 0, 100, 100)
window.add(oval)
Parameters:
  • gobj – The GObject to draw to this GWindow’s foreground layer.
  • x – The x-coordinate at which to draw the GObject.
  • y – The y-coordinate at which to draw the GObject.
add_to_region(gobj, region)[source]

Add an interactor to the control strip in a given region.

The interactor could also be a GLabel.

The region argument must be some region from the Region enum.

To add a button to the SOUTH region:

window = GWindow()
button = GButton("Click me!")
window.add_to_region(button, Region.SOUTH)
Parameters:
  • gobj – The interactor to add to a region.
  • region (Region) – The region to which the interactor will be added.
clear()[source]

Clear all content from this GWindow.

Both the background and foreground layers are cleared with this function.

close()[source]

Close this GWindow.

color

Get or set the marker color used for drawing.

When accessing the color, retrieve a GColor object.

TODO(sredmond): Document what sorts of colors can be supplied.

draw(gobj, x=None, y=None)[source]

Draw a GObject on this GWindow’s background layer.

The background layer is for static drawings that cannot be modified once drawn.

If both x and y are supplied, the object is moved to that location before drawing.

Parameters:
  • gobj – The GObject to draw to this GWindow’s background layer.
  • x – The x-coordinate at which to draw the GObject.
  • y – The y-coordinate at which to draw the GObject.
draw_line(x0, y0, x1, y1)[source]

Draw a line between the given coordinates.

The line’s color is this GWindow’s current marker color.

To draw a line from (0, 0) to (4, 1):

window = GWindow()
window.draw_line(0, 0, 4, 1)

To draw a line between two :class:`GPoint`s:

window = GWindow()
window.draw_line(*p0, *p1)
Parameters:
  • x0 – The x-coordinate of the starting point.
  • y0 – The y-coordinate of the starting point.
  • x1 – The x-coordinate of the ending point.
  • y1 – The y-coordinate of the ending point.
draw_oval(x, y, width, height)[source]

Draw the outline of an oval.

The oval’s upper-left corner has coordinates given by (x, y), and size given by (width, height). The corner of an oval is the corner of the oval’s bounding box.

The color of the oval’s outline will be this GWindow’s current marker color.

To draw an oval with an upper-left corner at (0, 0), a width of 300 pixels, and a height of 200 pixels:

window = GWindow()
window.draw_oval(0, 0, 300, 200)

To draw a circle centered at (410, 410) with radius 160:

window = GWindow()
window.draw_oval(410 - 160, 410 - 160, 160 * 2, 160 * 2)
Parameters:
  • x – The x-coordinate of the upper-left corner of the oval’s bounding box.
  • y – The y-coordinate of the upper-left corner of the oval’s bounding box.
  • width – The width of the oval in pixels.
  • height – The height of the oval in pixels.
draw_polar_line(x, y, r, theta)[source]

Draw a line from an initial point with a given length and polar direction.

The angle is measured in degrees counterclockwise from the positive x-axis.

This function also returns the ending point of the constructed line.

To draw a line of length 3 from the point (100, 100) at an angle of 60 degrees:

window = GWindow()
window.draw_polar_line(100, 100, 3, 60)
Parameters:
  • x – The x-coordinate of the starting point.
  • y – The y-coordinate of the starting point.
  • r – The length of the line to draw.
  • theta – The angle (measured from the positive x-axis) of the new line.
Returns:

The endpoint of the line.

draw_rect(x, y, width, height)[source]

Draw the outline of a rectangle.

The rectangle’s upper-left corner has coordinates given by (x, y) and size given by (width, height).

The color of the rectangle’s outline will be this GWindow’s current color.

To draw a rectangle with an upper-left corner at (0, 0), a width of 300 pixels, and a height of 200 pixels:

window = GWindow()
window.draw_rectangle(0, 0, 300, 200)
Parameters:
  • x – The x-coordinate of the upper-left corner of the rectangle’s bounding box.
  • y – The y-coordinate of the upper-left corner of the rectangle’s bounding box.
  • width – The width of the rectangle in pixels.
  • height – The height of the rectangle in pixels.
fill_oval(x, y, width, height)[source]

Draw a filled oval.

The oval’s upper-left corner has coordinates given by (x, y), and size given by (width, height). The corner of an oval is the corner of the oval’s bounding box.

The color of the oval will be this GWindow’s current marker color.

To draw a filled oval with an upper-left corner at (0, 0), a width of 300 pixels, and a height of 200 pixels:

window = GWindow()
window.fill_oval(0, 0, 300, 200)

To draw a filled circle centered at (410, 410) with radius 160:

window = GWindow()
window.fill_oval(410 - 160, 410 - 160, 160 * 2, 160 * 2)
Parameters:
  • x – The x-coordinate of the upper-left corner of the oval’s bounding box.
  • y – The y-coordinate of the upper-left corner of the oval’s bounding box.
  • width – The width of the oval in pixels.
  • height – The height of the oval in pixels.
fill_rect(x, y, width, height)[source]

Draw a filled rectangle.

The rectangle’s upper-left corner has coordinates given by (x, y) and size given by (width, height).

The color of the rectangle’s outline will be this GWindow’s current color.

To draw a rectangle with an upper-left corner at (0, 0), a width of 300 pixels, and a height of 200 pixels:

window = GWindow()
window.draw_rectangle(0, 0, 300, 200)
Parameters:
  • x – The x-coordinate of the upper-left corner of the rectangle’s bounding box.
  • y – The y-coordinate of the upper-left corner of the rectangle’s bounding box.
  • width – The width of the rectangle in pixels.
  • height – The height of the rectangle in pixels.
get_object_at(x, y)[source]

Return the topmost GObject containing the point (x, y) or None.

If no GObject contains the point (x, y), then return None.

This only searches through the foreground layer.

To search for a GObject at the point (41, 106):

window = GWindow()
gobj = window.get_object_at(41, 106)
if gobj is not None:
    print('We found an object!')
else:
    print('No object found.')
Parameters:
  • x – The x-coordinate of the point to examine.
  • y – The y-coordinate of the point to examine.
Returns:

The topmost GObject containing the given point, or None if no such object was found.

height

Get this GWindow’s height (in pixels).

remove(gobj)[source]

Remove a GObject from this GWindow.

If the GObject was in the foreground layer of the window, return True. Otherwise, return False, but otherwise do not any failure.

Parameters:gobj – The GObject to remove from this GWindow.
Returns:Whether the GObject was previously contained in this GWindow.
remove_from_region(gobj, region)[source]

Remove an interactor from the control strip in a given region.

The region argument must be some region from the Region enum.

Parameters:
  • gobj – The interactor to remove from a region.
  • region (Region) – The region from which the interactor will be removed.
set_region_alignment(region, align)[source]

Set an alignment for a given region.

Both the region and alignment arguments must be from the Region and Alignment enums respectively.

To CENTER the content in the SOUTH region:

window = GWindow()
window.set_region_alignment(Region.SOUTH, Alignment.CENTER)
Parameters:
  • region (Region) – The region in which to set alignment.
  • align (Alignment) – The alignment to set in the region.
title

Get or set this GWindow’s title.

Changing the title modifies the text in the top bar of the GWindow.

Usage:

window = GWindow()
window.title = "My Title"
print(window.title)
visible

Get or set this GWindow’s visibility.

TODO(sredmond): Document what, exactly, a GWindow’s visibility means.

Usage:

window = GWindow()
if not window.visible:
    window.visible = True
width

Get this GWindow’s width (in pixels).

class campy.graphics.gwindow.Region[source]

Bases: enum.Enum

Regions of a GWindow.

CENTER = 0
EAST = 1
NORTH = 2
SOUTH = 3
WEST = 4
campy.graphics.gwindow.exit_graphics()[source]

Close all graphical windows and forcibly exit the application.

campy.graphics.gwindow.pause(milliseconds)[source]

Pause for the given number of milliseconds.

This is useful for animation where the graphical updates would otherwise be too fast to observe.

To pause for half a second:

pause(500)
Parameters:milliseconds – The number of milliseconds to pause.
campy.graphics.gwindow.screen_height()[source]

Return the height of the entire display screen.

Returns:The height of the display screen in pixels.
campy.graphics.gwindow.screen_width()[source]

Return the width of the entire display screen.

Returns:The width of the display screen.