Lecture 17 Slides - Keyboard Events

1 of 13

More Events

1

CS 110

2 of 13

Variable Scope

2

3 of 13

Scope

  1. Scope — refers to the part of the program where a variable is accessible
  2. Lifetime — the duration for which the variable exists
  3. Global variable — a variable which is defined in the main body of a file (best avoided)
  4. Local variable — a variable which is defined inside a function. Not accessible from outside the function
    1. The parameter names in the function definition behave like local variables, but they contain the values that we pass into the function when we call it.

3

4 of 13

Scope 1: Consider the following program...

def demo_1(name):

greeting = 'Welcome, ' + name

demo_1('Jimmy')

print(greeting)

�What will print to the screen?

4

5 of 13

Scope 1: Local variables cannot be accessed outside function

def demo_1(name):

greeting = 'Welcome, ' + name

demo_1('Jimmy')

print(greeting)

TAKEAWAY: If you want access to a value after the function ends, you have to return it.

5

local variable

greeting variable was local to the demo_1 function. Throws error.

6 of 13

Scope 2: Which name will print to the screen?

name = 'Lindsay'

def demo_2(name):

print(name)

demo_2('Walter')

6

Global variable

Parameters are local variables. �They take precedence over global variables

7 of 13

Scope 2: Which name will print to the screen?

name = 'Lindsay'

def demo_2(name):

print(name)

demo_2('Walter')

TAKEAWAY: Local variables take precedence over global variables.

7

Global variable

Parameters are local variables. �They take precedence over global variables

8 of 13

Scope 3: Global variables can be accessed inside a function

name_1 = 'Lindsay'

def demo_3(name):

print(name)

print(name_1)

demo_3('Walter')

8

local variable

global variable

9 of 13

Scope 4: Global variables can updated with global keyword

name = 'Lindsay'

def modify_name(new_name):

global name

name = new_name

print(name)�modify_name('Walter')�print(name)

9

10 of 13

Scope 5: Without global keyword, Python assumes you’re creating a new local variable

�name = 'Lindsay'

def modify_name(new_name):

name = new_name

print(name)�modify_name('Walter')�print(name)

10

11 of 13

Back to Events

11

12 of 13

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:

  1. An event listener, which can detect that an event has occurred and the nature of it. Listeners notify your program that something has happened.
  2. 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.”

12

13 of 13

listen_for("LEFT-CLICK", do_something)

13

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