Docsity

Subclasses in Event-driven Programs - Introduction to Computing Using Python - Lecture Slides, Slides of Introduction to Computing

Partial preview of the text

Download Subclasses in Event-driven Programs - Introduction to Computing Using Python - Lecture Slides and more Slides Introduction to Computing in PDF only on Docsity!

Model

• Defines and

manages the data

• Responds to the

controller requests

View

• Displays model to

the user

• Provides interface

for the controller

Controller

• Updates model in

response to events

• Updates view with

model changes

Model-View-Controller Pattern

Calls the

methods or

functions of

Division

can apply

to classes

or modules

A Standard GUI Application

Update the display

No major computation

Animates the

application,

like a movie

Check for user input

Process that user input

Update the models

Must We Write this Loop Each Time?

while program_is_running:

# Get information from mouse/keyboard

# Handled by OS/GUI libraries

# Your code goes here

# Draw stuff on the screen

# Handled by OS/GUI libraries

Must We Write this Loop Each Time?

while program_is_running:

# Get information from mouse/keyboard

# Handled by OS/GUI libraries

# Your code goes here

# Draw stuff on the screen

# Handled by OS/GUI libraries

Why do we need to

write this each time?

Would like to “plug in” code

Callback Functions

• Given : predefined code that

calls some function

§ But function not defined

§ You want to replace it with

your function

• You redefine that function

§ By overriding it in a subclass

(do this in A7)

§ Or by storing a reference to

your function somewhere

(“registering” your callback)

§ Works the same either way

while program_running:

# Get input

# Your code goes here

callback()

# Draw

See callback.py

Example: Animation

• Callback: animate(…)

§ Called 60x a second

§ Moves back and forth

• Animate is a method

§ Associated with an object

§ Object has changing state

• Examples of state

§ Ellipse position

§ Current velocity

§ Current animation step

def animate(self,dt): """Animate the ellipse back & forth""" if self._steps == 0:

Initialize

… elif self._steps > ANIMATION_STEPS/2:

Move away

x = self._ellipse.pos[0] y = self._ellipse.pos[1] self._ellipse.pos = (x+self._vx,y+self._vy) self._steps = self._steps - 1 else: # Move back x = self._ellipse.pos[0] y = self._ellipse.pos[1] self._ellipse.pos = (x-self._vx,y-self._vy) self._steps = self._steps - 1

State Across Multiple Callbacks

• Sometimes have more than

one callback function

• Example: touch events

§ on_touch_down:" User presses mouse (or a finger); does not release § on_touch_up:" Releases mouse (or finger) § on_touch_move:" Moves mouse (or finger)

• State needed to track 

change in touch over time

See touch.py

Previous Touch Current Touch

State Across Multiple Callbacks

# None or previous touch

_anchor = None

def on_touch_down(self,touch):

# Track touch state

self._anchor = (touch.x,touch.y)

def on_touch_up(self,touch):

# Nothing to track

self._anchor = None

def on_touch_move(self,touch):

if not self._anchor is None:

self.drawLine(self._anchor[0], self._anchor[1],

touch.x,touch.y,LINE_COLOR)

self._anchor = (touch.x,touch.y)

See touch.py

Previous Touch Current Touch