Class GWindow
The GWindow class implements the background canvas for
the Portable Graphics Library.
Graphical applications begin by creating a new GWindow
object and then assemble a collage-like display by adding instances of
type GObject to the window.
By convention, the first statement in an application that uses the
Portable Graphics Library is
gw = GWindow(GWINDOW_WIDTH, GWINDOW_HEIGHT)
in which GWINDOW_WIDTH and GWINDOW_HEIGHT
are constants specifying the dimensions of the window in pixels.
As an example, the following program displays a blue rectangle on the
screen:
from pgl import GWindow, GRect
GWINDOW_WIDTH = 500
GWINDOW_HEIGHT = 200
def blue_rectangle():
gw = GWindow(GWINDOW_WIDTH, GWINDOW_HEIGHT)
rect = GRect(150, 50, 200, 100)
rect.set_color("Blue")
rect.set_filled(True)
gw.add(rect)
|
Constructor Summary |
GWindow()
Creates a new GWindow with a default preferred size. |
GWindow(width, height)
Creates a new GWindow and sets the size of its canvas
to the specified width and height. |
|
Method Summary |
add(gobj)
Adds the graphical object to this canvas. |
add(gobj, x, y)
Adds the graphical object to this canvas and sets its location
to the point (x, y). |
add_event_listener(type, fn)
Registers a new listener for events of the specified type occuring in the
window.
|
get_element(k)
Returns the graphical object at index k, numbering from back
to front in the z dimension. |
get_element_at(x, y)
Returns the topmost graphical object that contains the point
(x, y), or None if no such
object exists. |
get_element_count()
Returns the number of graphical objects stored in this GWindow. |
get_height()
Returns the height of the window in pixels. |
get_width()
Returns the width of the window in pixels. |
remove(gobj)
Removes a graphical object from the window. |
set_interval(fn, delay)
Starts an interval timer that calls fn every
delay milliseconds. |
set_timeout(fn, delay)
Starts a one-shot timer that calls fn after
delay milliseconds. |
GWindow()
- Creates a new
GWindow with a default size.
-
GWindow(width,
height)
- Creates a new
GWindow with the specified dimensions.
-
Usage: | gw = GWindow(width, height) |
Parameters: |
width | The width of the window in pixels
|
height | The height of the window in pixels.
|
|
add(gobj)
- Adds the graphical object to this canvas.
-
Usage: | gw.add(gobj) |
Parameter: |
gobj | The graphical object to add
|
|
add(gobj, x, y)
- Adds the graphical object to this canvas and sets its location
to the point (x, y).
-
Usage: | gw.add(gobj, x, y) |
Parameters: |
gobj | The graphical object to add
|
x | The new x-coordinate for the object
|
y | The new y-coordinate for the object
|
|
add_event_listener(type, fn)
- Registers a new listener for events of the specified type occuring
in the window.
The type parameter should be one of the strings
"
mousedown", "mouseup", "click",
"mousemove", "mousemove", or "drag".
-
Usage: | timer = gw.add_event_listener(type, fn) |
Parameter: |
type | The event type
|
fn | The callback function
|
|
get_element(k)
- Returns the graphical object at index k, numbering from back
to front in the the z dimension.
-
Usage: | gobj = gw.get_element(index) |
Parameter: |
index | The index of the component to return
|
|
Returns: | The graphical object at the specified index
|
get_element_at(x, y)
- Returns the topmost graphical object that contains the point
(x, y), or
None if no such
object exists.
-
Usage: | gobj = gw.get_element_at(x, y) |
Parameters: |
x | The x-coordinate of the point being tested
|
y | The y-coordinate of the point being tested
|
|
Returns: | The graphical object at the specified location, or
None if no such object exists
|
get_element_count()
- Returns the number of graphical objects stored in this
GWindow.
-
Usage: | n = gw.get_element_count() |
Returns: | The number of graphical objects in this GWindow
|
get_height()
- Returns the height of this window in pixels.
-
Usage: | height = gw.get_height() |
Returns: | The height of this window in pixels
|
get_width()
- Returns the width of this window in pixels.
-
Usage: | width = gw.get_width() |
Returns: | The width of this window in pixels
|
remove(gobj)
- Removes a graphical object from the window.
-
| |
Usage: | gw.remove(gobj) |
Parameter: |
gobj | The graphical object to remove
|
|
set_interval(fn, delay)
- Creates a new interval timer that calls
fn
every delay milliseconds.
-
Usage: | timer = gw.set_interval(fn, delay) |
Parameters: |
fn | The callback function
|
delay | The delay, measured in
milliseconds
|
|
Returns: | The GTimer object that implements the timer
|
set_timeout(fn, delay)
- Creates a new one-shot timer that calls
fn
after the specified delay, which is measured in milliseconds.
-
Usage: | timer = gw.set_timeout(fn, delay) |
Parameters: |
fn | The callback function
|
delay | The delay, measured in
milliseconds
|
|
Returns: | The GTimer object that implements the timer
|