Class Pen

class tkturtle.Pen(screen, position=(0, 0), color='red', speed=0)

Instances represent 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: Creates 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 or ints.

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

Pen.pencolor

The pen color of this pen.

The pen color is used for drawing lines and circles. 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).

Pen.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).

Pen.fill

The fill status of this pen.

If the fill status is True, then the pen will fill the insides of any polygon or circle subsequently traced by its drawLine or drawCircle 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.

Pen.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.

Pen.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.

Pen.x

The x-coordinate of this pen.

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

This attribute may not be (directly) altered

Pen.y

The y-coordinate of this pen.

To change the y coordinate, use one of the drawing methods.

This attribute may not be (directly) altered

Methods

Drawing Methods

Pen.drawLine(dx, dy)

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

param dx:change in the x position

Precondition: a float or int

param dy:change in the y position

Precondition: a float or int

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)

Pen.drawCircle(r)

Draw a circle of radius r centered on the pen.

param r:radius of the circle

Precondition: a float or int

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

Pen.drawTo(x, y)

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

param x:finishing x position for line

Precondition: a float or int

param y:finishing y position for line

Precondition: a float or int

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

Other Methods

Pen.move(x, y)

Moves the pen to given position without drawing.

param x:new x position for pen

Precondition: a float or int

param y:new y position for pen

Precondition: a float or int

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.

Pen.clear()

Deletes the pen’s drawings from the window.

This method does not move the pen or alter its attributes.

Pen.reset()

Deletes the pen’s drawings from the window.

This method re-centers the pen and resets all attributes to their default values.