ECS 10 Graphics Module
We are using a slightly modified version of the LiveWires beginners graphics
module.
This page describes the functions we will need from this module.
Remember you can see information on any function in IDLE
using the help command,
for instance, help(begin_graphics).
On a Mac, it seems like the graphics window does not close properly
when the program is run in IDLE.
You can still write your program in IDLE, and it will be helpful for
debugging (you can see where it crashes, etc), but the graphics window will
only close properly when the program is run from a terminal
(open a terminal window, go to the folder where the program is, and
type, for instance, "python stars.py").
Handling the graphics window
-
begin_graphics(width=640, height=480, background=Color.white, title=None)
Pops up the graphics window. If title="None" the title of the
window becomes "Graphics window".
-
status = sleep(secs)
Pauses to respond to window motions, getting killed, etc., and then
returns.
The argument secs should be the number of seconds to
pause (can be floating-point, for instance sleep(0.1)).
Returns a Boolean value (which need not be used), which is True if the
graphics window is still running and False if it has been killed while
the program was paused.
-
end_graphics()
Shut down the graphics window.
Defining graphics objects
-
set_Color(c)
Makes the current color be c. The color c can be one of the
pre-defined colors, Color.black, Color.white, Color.whatever,
where whatever can be: black, white, red, yellow, blue, green, orange,
purple, brown, grey, dark_red, dark_green, dark_blue, pink, light_green,
light_blue, light_grey, dark_grey.
Example: set_Color(Color.red)
-
circle(x, y, r, color=None, filled=False, lineWidth=3.0)
Draws a circle with center (x,y) and radius r. If color is None,
uses the current color. If filled is
False, just draws the outline. Use lineWidth to change the
width of the outline, if desired.
Example: circle(300,300,50,filled=True)
-
line(x0, y0, x1, y1, color=None, lineWidth=3.0)
Draws a line from point (x0,y0) to point (x1,y1).
If color is None,
uses the current color.
Use lineWidth to change the
width of the line, if desired.
Example: line(300,50,300,550,lineWidth=9.0)
-
box(x0, y0, x1, y1, color=None, filled=0, lineWidth=3.0)
Draws a box with lower-left corner (x0,y0) and upper-right corner
(x1,y1). If color is None,
uses the current color. If filled is
False, just draws the outline. Use lineWidth to change the
width of the outline, if desired.
Example: box(30,30,100,100,colour=Color.grey)
-
polygon(coords, color=None, closed=0, filled=0, lineWidth=3.0)
Draws a polygon. The arguement coords should be a tuple of two-tuples
containing the coorinates of the polygon.
If color is None,
uses the current color. If filled is
False, just draws the outline. Use lineWidth to change the
width of the outline, if desired.
Example: polygon( ((270,270),(330,270),(300,330)), filled=True)
-
text(str, x, y, size=12, color=None, serifs=False)
Writes text in string str, with position (x,y) as the lower-left corner.
The size is the height of the letters.
If color is None,
uses the current color. Use serifs to change between the two
possible fonts.
Example: text("Hi!",50,50,size=24)
-
clear_screen(colour=None)
Removes all objects from the graphics window.
If the argument color specifies a new background color; if it
is not given, the background remains the same color
it was last time.
Example: clear_screen()
Moving objects
-
allow_movables()
After calling allow_movables, the commands circle, line, box and polygon
return labels which can be used to move or delete the object.
Example:
allow_movables()
#After calling allow_movables(), box returns a label for the box it draws
newBox = box(30,30,100,100,color=Color.grey)
-
forbid_movables()
After calling forbid_movables, the commands circle, line, box and polygon
no longere return labels which can be used to move or delete the object.
Example:
forbid_movables()
-
remove_from_screen(object)
Removes an object from the screen. The required argument object is the
label of an already-drawn graphics object.
Example:
remove_from_screen(newBox)
# Removes box added in example for allow_movables()
-
move_by(object,x,y)
Moves an object in the graphics window.
The required argument object is the
label of an already-drawn graphics object.
The arguemnts x and y specify how much the object should move in the x and
y directions.
Example:
move_by(newBox,5,10)
# Moves box by 5 in the x direction and 10 in the y direction.
Mouse functions
Under Construction