Jed Rembold & Fred Agbo
February 21, 2024
Consider the simple program below, where we’ve imported the basics and some of our helper functions
def draw_dots():
def click_action(event):
c = create_filled_rect(
event.get_x(), event.get_y(),
10,10, random_color())
gw.add(c)
gw = GWindow(500, 500)
gw.add_event_listener("click", click_action)
The click_action
function specifies
what to do when the mouse is clicked
gw
variable since it is in the enclosing function and thus in the
closure.The last line of our example function:
gw.add_event_listener("click", click_action)
tells the graphics window (gw
) to call
the click_action
function whenever a mouse
“click” occurs within the window.
When the user clicks the mouse, the graphics window, in essense,
calls the client back to let them know that a click has occured. Thus,
functions such as click_action
are known as
callback functions.
The parameter event
given to the
callback function is a special data structure called a mouse
event, which contains details about the specifics of the event that
triggered the action.
Name | Description |
---|---|
"click" |
The user clicks the mouse in the window |
"dblclk" |
The user double-clicks the mouse in the window |
"mousedown" |
The user presses the mouse button down |
"mouseup" |
The user releases the mouse button |
"mousemove" |
The user moves the mouse |
"drag" |
The user moves the mouse with the button down |
draw_dots()
function, you’ll notice that nothing happensgw.add_event_listener("click", click_action)
gw.add_event_listener("dblclk", dblclk_action)
from pgl import GWindow, GLine
WIDTH = 500
HEIGHT = 500
def draw_lines():
def mousedown_event(e):
x = e.get_x()
y = e.get_y()
line = GLine(x,y,x,y)
gw.add(line)
def drag_action(e):
line.set_end_point(e.get_x(), e.get_y())
gw = GWindow(WIDTH, HEIGHT)
line = None
gw.add_event_listener("mousedown", mousedown_event)
gw.add_event_listener("drag", drag_action)
if __name__ == '__main__':
draw_lines()
nonlocal
keyword, which allows you to state that a specific variable is
not local, but it tends to just confuse studentsGWindow
object (mostly commonly
named gw
)!gw
objectgw.my_attribute_name = some_cool_value
Just refer to the object and attribute name:
print(gw.my_attribute_name)
from pgl import GWindow, GLine
WIDTH = 500
HEIGHT = 500
def draw_lines():
def mousedown_event(e):
x = e.get_x()
y = e.get_y()
gw.line = GLine(x,y,x,y)
gw.add(gw.line)
def drag_action(e):
gw.line.set_end_point(e.get_x(), e.get_y())
gw = GWindow(WIDTH, HEIGHT)
gw.line = None
gw.add_event_listener("mousedown", mousedown_event)
gw.add_event_listener("drag", drag_action)
if __name__ == '__main__':
draw_lines()
Created with
gw.set_timeout(function, delay)
where function
is the callback function
and delay
is the time interval in
milliseconds
Created with
gw.set_interval(function, delay)
GTimer
object that
identifies the timer, and can be stopped by invoking the
.stop()
method on that timerdef moving_square():
def step():
square.move(dx, dy)
if square.get_x() > 500:
timer.stop()
gw = GWindow(500, 200)
dx = 1
dy = 0
square = create_filled_rect(12, 100, 24, 24, "red")
gw.add(square)
timer = gw.set_interval(step, 20)