Lecture 15 Slides - Review + Events and Listeners

1 of 10

Listening for Inputs

CS 110

1

2 of 10

Reminders and Announcements

  • Ex 5 due tonight (hints available on edSTEM!)
  • We'll talk about P1 on Monday (due in two Fridays)
    • Ex6 will ask you to complete the first half of P1

Next Week

  • Monday - Short Circuiting Loops
  • Wednesday - Keyboard Events + Scope (Pre-Recorded + MQ 10) + Tutorial 5
  • Friday - Using Loops for Data + MQ 11
    • Ex 6 due

3 of 10

quack

3

4 of 10

Problem 1: Infinite Loops

4

5 of 10

counter = 5

while counter < 5:

print(counter)

counter = counter + 1

The loop never begins (condition starts off as False)

Problem 2: Loops that aren't

5

6 of 10

INPUT AND EVENTS

Expanding past just using the Python interpreter

6

7 of 10

Examples of Inputs

There are many different kinds of inputs, which should all be very familiar to you:�

  • Clicking a button
  • Dragging, swiping, rotating
  • Using sensors
  • Reading data from files or databases
  • Double-clicking a program to start it

7

8 of 10

Events: Ways of Dealing with User Input

Events allow programs to listen to particular events (clicks, drags, etc.) and execute parts of a program based on the input. Events have two parts:

  • An event listener, which can detect that an event has occurred and the nature of it. Listeners notify your program that something has happened.
  • An event handler (the function), which is the block of code that responds to an event when is triggered by executing a code block. Event handlers are sometimes called “callbacks.”

8

9 of 10

setup_listener("LEFT-CLICK", do_something)

9

We use this function to set the listener up.

A function name to run when the event happens (handler) that is defined to have exactly 1 input

Setting an Event Listener on the Pop-up Window

The event to listen for (a special string specified by Python)

Options as of Week 6 (all strings) :

LEFT-CLICK RIGHT-CLICK ALT-CLICK LEFT-DRAG

10 of 10

Demos

lecture15/event_demos/

10