So far, all operations between or on objects have used symbols to
indicate the operation
The + sign, for instance
Going forward, we will begin to see more examples of operations
on objects that use receiver syntax
In receiver syntax, we specify the object to act on, followed by
a dot and then a predefined function (called a method here)
name
obj.method_name()
This is like you are running this special function on the object, so
you need the () at the
end
Some methods also allow arguments, to influence exactly how the
operation will proceed
An Approach to Success
Each project is accompanied by a highly detailed guide: read
it!
Explains background ideas so that you can understand the big picture
of what you are needing to do
Also included a breakdown of individual milestones
A milestone is a discrete checkpoint that you should ensure
is working (and that you understand!) before moving on
Projects are all about managing complexity. If you start trying to
implement milestones out of order, you are asking for disaster
Don’t let yourself get overwhelmed by scale. Focus on one particular
milestone at a time, which should involve focusing only on a small part
of your overall code
The Worth of a Picture
There comes a time when reading and entering text on a terminal
doesn’t cut it
Maybe you need more complicated input
Maybe you need a more complicated interface that pure text can
manage
Maybe you have output that can not be shown as text
Standard Python really only deals with the terminal interface
Lots of outside libraries give Python more visual input/output
Turtle
Matplotlib
Tkinter ← PGL
PyGame
Arcade
The Portable Graphics Library
Built atop Tkinter
The library (pgl.py) is available on the
website
Put it in the same folder as your code, and then you can import
it
Operates on the idea of a collage or cork-board
Note that newer objects can obscure older objects. This layering
arrangement is called the stacking order.
The Pieces
At its simplest then, we have two main parts:
The window (or felt-board/cork-board)
Created with the GWindow function
Takes two arguments: a width and a height in pixels
The contents
A wide assortment of shapes that can be added to the scene
Control over where they are placed, how large they are, what color
they are, etc
Here string_to_add is the text you
want to display, and x_location and
y_location are the (x,y) coordinates of
where you want to place the origin of the label.
Label Geometry
The GLabel class relies on some
geometrical concepts that are derived from classical typesetting
The baseline is the imaginary line on which the characters
rest
The origin is the point on the baseline at which the text
begins
The width is the horizontal distance from the origin to the
end of the text
The height of the font is the distance between adjacent
baselines
The ascent is the distance the characters rise above the
baseline
The descent is the distance the characters drop below the
baseline
Interacting with Labels
A GLabel has several special methods
that you can use to interact with it
You can use: get_width(),
get_height(),
get_ascent(), and
get_descent() methods to obtain the
geometric properties
You can set a special font for the label using
labelname.set_font(font)
Where font is a string comprised of
the following elements:
The font style, which is usually blank or
italic
The font weight, which is usually blank or
bold
The font size, which is a number followed by the units
(typically pt,
px, or em)
The font family, which is the name of the font. Because
what fonts are available can differ from machine to machine, the family
is usually a sequence of fonts separated by commas
The font family sequence usually ends with a standard family
(serif,
sans-serif, or
monospace) to ensure that the label can
display