Jed Rembold
September 29, 2025
Which of the below images would be produced by the following code?
gw = GWindow(200, 200)
for c in range(0, 10):
for r in range(0, 10):
rect = GRect(20*c, 20*r, 20, 20)
if (r + c) % 2 != 0:
rect.set_filled(True)
gw.add(rect)
Sometimes you need to add some text to the window
Can display any string using GLabel
using the following format:
msg = GLabel(|||string to add|||, |||x location|||, |||y location|||)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.
GLabel 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
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:
italicboldpt,
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)
GLabelGLabel 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 = gw.get_width() / 2 - msg.get_width() / 2
y = gw.get_height() / 2 + msg.get_ascent() / 2
gw.add(msg, x, y)
def |||function_name|||( |||parameter_list||| ):
|||body of the function|||
Functions that return a Boolean value are called predicate functions
def is_divisible_by(x, y):
return x % y == 0Once you have defined a predicate function, you can use it in any conditional expression!
for i in range(1, 100):
if is_divisible_by(i, 7):
print(i)Don’t complicate your code for no reason!
Work directly with the boolean values when possible
Try not to code patterns like the following:
def is_divisible_by(x, y):
if x % y == 0:
return True
else:
return False
for i in range(1, 100):
if is_divisible_by(i,7) == True:
print(i)So far we have used a positional way to assign arguments to parameters
>>> def func( first, second, third ):
print( first, second, third )
>>> func(1,2,3)
1 2 3
>>> func(2,6,4)
2 6 4
Arguments may also be specified by a keyword, in which the caller precedes the argument with a parameter name and equals sign
Always stores the argument value in the specified parameter
>>> def func( first, second, third ):
print( first, second, third )
>>> func(third=4, first=2, second=6)
2 6 4Keyword arguments can appear in any order
Python allows you to specify a default value for a parameter, which it will use if an argument is not directly supplied
Do so by adding an equals sign and a value after the parameter name (in the function definition)
def introduction(name='Jed', age=40):
print(f'My name is {name} and I am {age}')
If providing any arguments after a default parameter, you must indicate them through keywords
>>> introduction()
My name is Jed and I am 40
>>> introduction('Bob', 25)
My name is Bob and I am 25
>>> introduction('Larry')
My name is Larry and I am 40
>>> introduction(age=68)
My name is Jed and I am 68
You can return any type of variable
from a function, including GObject graphical
objects
Can be useful to write simple functions that bundle together common tasks
For instance, to create a filled circle centered at some location:
def make_filled_circ(x_cent, y_cent, radius, color='black'):
circle = GOval( x_cent-radius, y_cent-radius,
2*radius, 2*radius)
circle.set_color(color)
circle.set_filled(True)
return circleimport so generally don’t want extraneous
print statements or to be running any code directlyimport the library in.py part of the extensionfrom pgl import GRect, GLabel
import random
def create_filled_rect(
x_cent, y_cent, width, height, fill_col='black', border_col=None
):
"""
Creates a GRect object with the desired fill color.
If a border color is specified, also draws the
border in the desired color.
"""
rect = GRect(x_cent-width/2, y_cent-height/2, width, height)
rect.set_filled(True)
if border_col is None:
rect.set_color(fill_col)
else:
rect.set_color(border_col)
rect.set_fill_color(fill_col)
return rect
def random_color():
"""
Returns a random opaque color as a hex string.
"""
color = "#"
for i in range(6):
color += random.choice("0123456789ABCDEF")
return color
def create_centered_label(x_cent, y_cent, text, font=None):
"""
Creates a GLabel object and centers it on the coordinates
x_cent and y_cent.
"""
label = GLabel(text)
if font is not None:
label.set_font(font)
label.set_location(x_cent - label.get_width() / 2,
y_cent + label.get_ascent() / 2 )
return label
def Vegas(x):
y = 2
for i in range(5):
x += y
return x
x = 3
z = Vegas(x)
print('z =', z)
print('x =', x)
Consider the code to the left. When the final value of
x is printed, what will its value be?
NoneVegas stays in
Vegas…We’ll annotate the stack frames by hand as the earlier code runs:
def Vegas(x):
y = 2
for i in range(5):
x += y
return x
x = 3
z = Vegas(x)
print('z =', z)
print('x =', x)
return, compute the return value and
substitute that value in place of the function callRiddle me this. What would be the printed value of z at the end of the code to the right?
def f(x,y):
z = (x + 3) ** 2
return y + z
x = 1
z = x + f(y=x,x=2)
print(z)
abs,
str, print,
etc.def func1(x,y):
return z + func2(x,y)
def func2(x,y):
def func3(x):
return (y + x) ** 2
z = x - func3(y)
return z - y
z = 1
print(func1(2,z))
In Python, assigning any value to a variable means that the variable is assumed to be local
Can lead to issues though:
def increment():
x = x + 1
x = 0
increment()There are a few ways to address this, but we’ll focus on one in particular when it comes to PGL