Jed Rembold & Fred Agbo
February 14, 2024
You can check if an individual element is in a particular
sequence of elements using the in
keyword
"1" in "12345"
You can change the case of all letters in a string using
upper()
or
lower()
methods
lowered = "ABCDEF".lower()
uppered = "abcDEF".upper()
GWindow(width, height)
in last classFunctions to create simple geometric objects:
GRect( x, y, width, height )
GOval( x, y, width, height )
GLine( x1, y1, x2, y2 )
GObject
Hierarchy
GObject
class represents the
collection of all graphical objectsGFillableObject
class represents
those that have a fillable interiorGWindow
gw = GWindow(width, height)
GWindow
object:gw.add(object) |
Adds an object to the window |
gw.add(object, x, y) |
Adds an object to the window after moving it to (x,y) |
gw.remove(object) |
Removes an object from the window |
gw.get_width() |
Returns the width of the graphics window in pixels |
gw.get_height() |
Returns the height of the graphics window in pixels |
GObject
sobject
is the name of any specific
instance.object.get_x() |
Returns the x coordinate of this object |
object.get_y() |
Returns the y coordinate of this object |
object.get_width() |
Returns the width of this object |
object.get_height() |
Returns the height of this object |
object.set_color(color) |
Sets the color of the object to the specified color |
GFillableObject
sobject.set_filled(bool) |
Sets the fill state of the object |
object.set_fill_color(color) |
Sets the color to be used to fill the interior, otherwise same as the outer line |
object.get_fill_color() |
Gets the current color used to display the object interior |
object.is_filled() |
Returns True or False depending on whether the object is currently filled |
gw = GWindow(400, 400)
head = GOval(20, 20, 360, 360)
head.set_fill_color("yellow")
head.set_filled(True)
gw.add(head)
reye = GOval(110, 100, 40, 40)
reye.set_filled(True)
gw.add(reye)
leye = GOval(250, 100, 40, 40)
leye.set_filled(True)
gw.add(leye)
mouth = GLine(150, 250, 250, 250)
mouth.set_line_width(5)
gw.add(mouth)
GLabel
using the following format:msg = GLabel(string_to_add, x_location, y_location)
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 stringGLabel
class relies on some
geometrical concepts that are derived from classical typesetting
GLabel
has several special methods
that you can use to interact with it
get_width()
,
get_height()
,
get_ascent()
, and
get_descent()
methods to obtain the
geometric propertieslabelname.set_font(font)
italic
bold
pt
,
px
, or em
)serif
,
sans-serif
, or
monospace
) to ensure that the label can
displaygw = GWindow(500, 200)
msg = GLabel("hello world!", 50, 100)
msg.set_font("italic bold 80px 'times new roman'")
gw.add(msg)
GLabel
GLabel
without setting its
location.set_font()
method to set the
desired font (which could change the size)GLabel
at the newly calculated
positiongw = GWindow(500, 200)
msg = GLabel("hello world!")
msg.set_font("italic bold 20px 'times new roman'")
x = 250 - msg.get_width() / 2
y = 100 + msg.get_ascent() / 2
gw.add(msg, x, y)
How could we think about decomposing the problem of drawing the below
house?