Class Pen

class cturtle.Pen(screen, position=(0, 0), color='red', heading=180, speed=0)

Instance is a graphics pen.

A graphics pen is like a turtle except that it does not have a heading, and there is no drawmode attribute. Instead, the pen relies on explicit drawing commands such as drawLine or drawCircle.

Another difference with the pen is that it can draw solid shapes. The pen has an attribute called fill. When this attribute is set to True, it will fill the insides of any polygon traced by its drawLine method. However, the fill will not be completed until fill is set to False, or the move method is invoked.

Constructor: Create a new pen to draw on the given screen.

param screen:window object that pen will draw on.

Precondition: object of type Window.

param position:initial pen position (origin is screen center)

Precondition: 2D tuple of floats.

param color:initial pen and fill color (default red)

Precondition: either a string with a color name, a 3 element tuple of floats between 0 and 1 (inclusive), or an object in an additive color model (e.g. RGB or HSV).

param speed:initial turtle speed (default 0)

Precondition: a int between 0 and 10, inclusive

The argument screen is not optional.

Attributes

pencolor

The pen color of this pen.

The pen color is used for drawing lines. All subsequent draw commands draw using this color. If the color changes, it only affects future draw commands, not past ones.

This color is only used for lines and the border of circles. It is not the color used for filling in solid areas (if the fill attribute is True). See the attribute fillcolor for solid shapes.

Invariant: Value must be either a string with a color name, a 3 element tuple of floats between 0 and 1 (inclusive), or an object in an additive color model (e.g. RGB or HSV).

fillcolor

The fill color of this turtle.

The fill color is used for filling in solid shapes. If the fill attribute is True, all subsequent draw commands fill their insides using this color. If the color changes, it only affects future draw commands, not past ones.

This color is only used for filling in the insides of solid shapes. It is not the color used for the shape border. See the attribute pencolor for the border color.

Invariant: Value must be either a string with a color name, a 3 element tuple of floats between 0 and 1 (inclusive), or an object in an additive color model (e.g. RGB or HSV).

fill

The fill status of this pen.

If the fill status is True, then the pen it will fill the insides of any polygon subsequently traced by its drawLine method. If the attribute changes, it only affects future draw commands, not past ones. Switching this attribute between True and False allows the pen to draw both solid and hollow shapes.

Invariant: Value must be an bool.

speed

The animation speed of this pen.

The speed is an integer from 0 to 10. Speed = 0 means that no animation takes place. The drawLine and drawCircle methods happen instantly with no animation.

Speeds from 1 to 10 enforce increasingly faster animation of line drawing. 1 is the slowest speed while 10 is the fastest (non-instantaneous) speed.

Invariant: Value must be an integer value in the range 0..10.

visible

Indicates whether the pen’s icon is visible.

Drawing commands will still work while the pen icon is hidden. There will just be no indication of the pen’s current location on the screen.

Invariant: Value must be a bool

Immutable Attributes

These attributes may be read (e.g. used in an expression), but not altered.

x

The x-coordinate of this pen.

To change the x coordinate, use of the drawing methods.

This attribute may not be (directly) altered

y

The y-coordinate of this pen.

To change the x coordinate, use of the drawing methods.

This attribute may not be (directly) altered

Methods

Drawing Methods

drawLine(dx, dy)

Draw a line segment (dx,dy) from the current pen position

param dx:change in the x position

Precondition: a float

param dy:change in the y position

Precondition: a float

The line segment will run from (x,y) to (x+dx,y+dy), where (x,y) is the current pen position. When done, the pen will be at position (x+dx,y+dy)

drawCircle(r)

Draw a circle of radius r centered on the pen.

param r:radius of the circle

Precondition: a float

The center of the circle is the current pen coordinates. When done, the position of the pen will remain unchanged

drawTo(x, y)

Draw a line from the current pen position to (x,y)

param x:finishing x position for line

Precondition: a float

param y:finishing y position for line

Precondition: a float

When done, the pen will be at (x, y).

Other Methods

move(x, y)

Moves the pen to given position without drawing.

param x:new x position for pen

Precondition: a float

param y:new y position for pen

Precondition: a float

If the fill attribute is currently True, this method will complete the fill before moving to the new region. The space between the original position and (x,y) will not be connected.

undo()

Undo the last action by this pen

clear()

Delete the pen’s drawings from the window, but do not move the pen or alter its attributes.

reset()

Delete the pen’s drawings from the hwindow, re-center the pen and reset all attributes to their default values.

Table Of Contents

Previous topic

Class Turtle

This Page