Window¶
import introcs.turtle
A window is a GUI canvas that supports turtle (Turtle
or Pen
) graphics.
You construct a window before adding the turtle or pen. A window can have as many
turtles or pens as you want at the same time. It can also have no turtles or pens.
While windows may be resized, your should take great care when resizing them while drawing.
Since the origin is typically the center of the window, resizing a window will shift the
drawing implements (Turtle
and Pen
objects) to a new position. For
the most part, this should preserve coordinates, but round-off error may be an issue.
Because of the overhead, this class is not included in the top level of the introcs
module. It must be imported separately as part of the introcs.turtle
module.
Constructor¶
- class introcs.turtle.Window(x=50, y=50, width=700, height=700, scale=1)¶
An instance is a GUI windows that support turtle graphics
You should construct a
Window
object before constructing aTurtle
orPen
. While you should only need oneWindow
object at any given time, you may have as many as you want. Deleteing aWindow
object will close the window, making the associated turtles and pens invalid.- Parameters
x (
int
>= 0) – initial x coordinate (default 50)y (
int
>= 0) – initial y coordinate (default 50)width (
int
> 0) – initial window width (default 700)height (
int
> 0) – initial window height (default 700)scale (
float
> 0) – initial window scale (INGORED)
Mutable Attributes¶
- Window.x¶
The x coordinate for top left corner of window
Screen coordinates have their origin in the top left corner, with y increasing downwards.
invariant: x must be an
int
>= 0
- Window.y¶
The y coordinate for top left corner of window
Screen coordinates have their origin in the top left corner, with y increasing downwards.
invariant: y must be an
int
>= 0
- Window.width¶
The width of the drawing canvas in pixels
The width specified is the width of the internal drawing canvas. The width does not include the border or window frame (title, close buttons, etc.)
invariant: width must be an
int
> 0
- Window.height¶
The height of the drawing canvas in pixels
The height specified is the height of the internal drawing canvas. The height does not include the border or window frame (title, close buttons, etc.)
invariant: height must be an
int
> 0
- Window.title¶
The title displayed at top of window bar
invariant: title must be a
str
- Window.resizable¶
Whether or not the Window supports user resizing
invariant: resizable must be a
bool
Immutable Attributes¶
These attributes may be read (e.g. used in an expression), but not altered.
- Window.turtles¶
The tuple of all turtles attached to this Window
This attribute may not be altered directly
- Window.pens¶
The tuple of all pens attached to this Window.
This attribute may not be altered directly
Methods¶
All of these methods modify the underlying window object.
- Window.setPosition(x, y)¶
Sets the position of this Window
Screen coordinates have their origin in the top left corner, with y increasing downwards.
- Parameters
x – the left edge of the window
y – the top edge of the window
- Window.setSize(width, height)¶
Sets the size for this Window
The size specified is the actual the size of the internal drawing canvas. The size does not include the border or window frame (title, close buttons, etc.)
- Parameters
width (
int
> 0) – the window widthheight (
int
> 0) – the height width
- Window.setMaxSize(width, height)¶
Sets the maximum size for this Window
The size specified is the maximum the size of the internal drawing canvas. The size does not include the border or window frame (title, close buttons, etc.)
Any attempt to resize a dimension beyond the maximum size will fail.
- Parameters
width (
int
> 0) – the maximum Window widthheight (
int
> 0) – the maximum Window height
- Window.setMinSize(width, height)¶
Sets the minimum size for this window
The size specified is the minimum the size of the internal drawing canvas. The size does not include the border or window frame (title, close buttons, etc.)
Any attempt to resize a dimension below the minimum size will fail.
- Parameters
width (
int
> 0) – the minimum Window widthheight (
int
> 0) – the minimum Window height
- Window.iconify()¶
Shrinks the Window down to an icon, effectively hiding it
- Window.deiconify()¶
Expands the Window from an icon so that it is visible
- Window.flush()¶
Displays any pending drawing commands on the screen.
This command is necessary when
Turtle
orPen
speed is set to 0. When that happens, the drawing tool will not force a refresh until this command is executed.
- Window.clear()¶
Erases the contents of this Window
All Turtles and Pens are eliminated from the Window. Any attempt to use a previously created
Turtle
orPen
will fail.
- Window.dispose()¶
Closes the graphics Window, deleting all assets.
- Window.beep()¶
Plays an OS specific alert sound
Errors¶
- class introcs.turtle.window.AttachmentError¶
Instance is an error for an illegal drawing operation.
This error is used to prevent drawing tools from using commands when they are unhooked from their window.