CMU 15-112: Fundamentals of Programming and Computer Science
Class Notes: Event-Based Animations in Tkinter
Part 1: Getting Started with MVC
- Installing required modules (PIL/Pillow and Requests)
- Our First Example: A KeyPress Counter
- Model-View-Controller (MVC)
- Legal event.key values
- Moving a Dot with Key Presses
- Moving a Dot with Arrows
- Moving a Dot with Arrows and Bounds
- Moving a Dot with Arrows and Wraparound
- Moving a Dot in Two Dimensions
Notes:
- To run these examples, first download cmu_112_graphics.py and be sure it is in the same folder as the file you are running.
- That file has version numbers. As we release updates, be sure you are using the most-recent version!
- This is a relatively new animation framework, with some changes from previous semesters. So it is similar to previous semesters, but different in important ways. Be aware of this if you are reviewing previous semesters' materials!
- As with Tkinter graphics, the examples here will not run using Brython in your browser.
- Installing required modules (PIL/Pillow and Requests)
To use cmu_112_graphics.py, you need to have some modules installed. If they are not installed, you will see a message like this (or a similar one for "requests" instead of "PIL"):********************************************************** ** Cannot import PIL -- it seems you need to install pillow ** This may result in limited functionality or even a runtime error. **********************************************************
You can try to use 'pip' to install the missing modules, but it can be complicated making sure you are installing these modules for the same version of Python that you are running. Here are some more-reliable steps that should work for you:
Important Hint: in the steps below, you will use the terminal (on Mac) or command prompt (on Windows). In each case, this is not the terminal in VS Code!- To get a Command Prompt on Windows, hit the Windows key, and then type in 'command' (or just 'cmd').
- To get a Terminal on a Mac, click on the Spotlight Search and type in 'terminal'.
With that, here are the steps:- For Windows:
- Run this Python code block in your main Python file (it will print the commands you need to paste into your command prompt):
import sys print(f'"{sys.executable}" -m pip install pillow') print(f'"{sys.executable}" -m pip install requests')
- Open Command Prompt as an administrator user (right click - run as administrator)
- Copy-paste each of the two commands printed in step 1 into the command prompt you opened in step 2.
- Close the command prompt and close Python.
- Re-open Python, and you're set (hopefully)!
- Run this Python code block in your main Python file (it will print the commands you need to paste into your command prompt):
- For Mac or Linux:
- Run this Python code block in your main Python file (it will print the commands you need to paste into your command prompt):
import sys print(f'sudo "{sys.executable}" -m pip install pillow') print(f'sudo "{sys.executable}" -m pip install requests')
- Open Terminal
- Copy-paste each of the two commands printed in step 1 into the
command prompt you opened in step 2.
- If you see a lock and a password is requested, type in the same password that you use to log into your computer.
- Close the terminal and close Python.
- Re-open Python, and you're set (hopefully)!
- Run this Python code block in your main Python file (it will print the commands you need to paste into your command prompt):
If these steps do not work for you, please go to OH and we will be happy to assist. - Our First Example: A KeyPress Counter
from cmu_112_graphics import * def appStarted(app): app.counter = 0 def keyPressed(app, event): app.counter += 1 def redrawAll(app, canvas): canvas.create_text(app.width/2, app.height/2, text=f'{app.counter} keypresses', font='Arial 30 bold', fill='black') runApp(width=400, height=400)
- Model-View-Controller (MVC)
Note:- We will write animations using the Model-View-Controller (MVC) paradigm.
- The model contains all the data we need for the animation.
We can store the model in the
app
object's attributes.- In the example above,
app.counter
is our model.
- In the example above,
- The view draws the app using the values in the model.
- In the example above,
redrawAll
is our view.
- In the example above,
- The controller responds to keyboard, mouse, timer and other
events and updates the model.
- In the example above,
keyPressed
is our controller.
- In the example above,
- You never call the view or the controllers. The animation framework
calls these for you.
- In the example above,
we never call
redrawAll
orkeyPressed
. They are called for us.
- In the example above,
we never call
- Controllers can only update the model, they cannot update the view.
- In the example above,
keyPressed
cannot callredrawAll
.
- In the example above,
- The view can never update the model.
- In the example above,
redrawAll
cannot changeapp.counter
or any other values in the model.
- In the example above,
- If you violate these rules, it is called an MVC Violation. If that happens, your code will stop running and will display the runtime error for you.
- Legal event.key values
# Note: Tkinter uses event.keysym for some keys, and event.char # for others, and it can be confusing how to use these properly. # Instead, cmu_112_graphics replaces both of these with event.key, # which simply works as expected in all cases. from cmu_112_graphics import * def appStarted(app): app.message = 'Press any key' def keyPressed(app, event): app.message = f"event.key == '{event.key}'" def redrawAll(app, canvas): canvas.create_text(app.width/2, 40, text=app.message, font='Arial 30 bold', fill='black') keyNamesText = '''Here are the legal event.key names: * Keyboard key labels (letters, digits, punctuation) * Arrow directions ('Up', 'Down', 'Left', 'Right') * Whitespace ('Space', 'Enter', 'Tab', 'BackSpace') * Other commands ('Delete', 'Escape')''' y = 80 for line in keyNamesText.splitlines(): canvas.create_text(app.width/2, y, text=line.strip(), font='Arial 20', fill='black') y += 30 runApp(width=600, height=400)
- Moving a Dot with Key Presses
- Moving a Dot with Arrows
from cmu_112_graphics import * def appStarted(app): app.cx = app.width/2 app.cy = app.height/2 app.r = 40 def keyPressed(app, event): if (event.key == 'Left'): app.cx -= 10 elif (event.key == 'Right'): app.cx += 10 def redrawAll(app, canvas): canvas.create_text(app.width/2, 20, text='Move dot with left and right arrows', fill='black') canvas.create_oval(app.cx-app.r, app.cy-app.r, app.cx+app.r, app.cy+app.r, fill='darkGreen') runApp(width=400, height=400)
- Moving a Dot with Arrows and Bounds
# This version bounds the dot to remain entirely on the canvas from cmu_112_graphics import * def appStarted(app): app.cx = app.width/2 app.cy = app.height/2 app.r = 40 def keyPressed(app, event): if (event.key == 'Left'): app.cx -= 10 if (app.cx - app.r < 0): app.cx = app.r elif (event.key == 'Right'): app.cx += 10 if (app.cx + app.r > app.width): app.cx = app.width - app.r def redrawAll(app, canvas): canvas.create_text(app.width/2, 20, text='Move dot with left and right arrows', fill='black') canvas.create_text(app.width/2, 40, text='See how it is bounded by the canvas edges', fill='black') canvas.create_oval(app.cx-app.r, app.cy-app.r, app.cx+app.r, app.cy+app.r, fill='darkGreen') runApp(width=400, height=400)
- Moving a Dot with Arrows and Wraparound
# This version wraps around, so leaving one side enters the opposite side from cmu_112_graphics import * def appStarted(app): app.cx = app.width/2 app.cy = app.height/2 app.r = 40 def keyPressed(app, event): if (event.key == 'Left'): app.cx -= 10 if (app.cx + app.r <= 0): app.cx = app.width + app.r elif (event.key == 'Right'): app.cx += 10 if (app.cx - app.r >= app.width): app.cx = 0 - app.r def redrawAll(app, canvas): canvas.create_text(app.width/2, 20, text='Move dot with left and right arrows', fill='black') canvas.create_text(app.width/2, 40, text='See how it uses wraparound on the edges', fill='black') canvas.create_oval(app.cx-app.r, app.cy-app.r, app.cx+app.r, app.cy+app.r, fill='darkGreen') runApp(width=400, height=400)
- Moving a Dot in Two Dimensions
# This version moves in both x and y dimensions. from cmu_112_graphics import * def appStarted(app): app.cx = app.width/2 app.cy = app.height/2 app.r = 40 def keyPressed(app, event): if (event.key == 'Left'): app.cx -= 10 elif (event.key == 'Right'): app.cx += 10 elif (event.key == 'Up'): app.cy -= 10 elif (event.key == 'Down'): app.cy += 10 def redrawAll(app, canvas): canvas.create_text(app.width/2, 20, text='Move dot with up, down, left, and right arrows', fill='black') canvas.create_oval(app.cx-app.r, app.cy-app.r, app.cx+app.r, app.cy+app.r, fill='darkGreen') runApp(width=400, height=400)
- Moving a Dot with Arrows
- Moving a Dot with Mouse Presses
from cmu_112_graphics import * def appStarted(app): app.cx = app.width/2 app.cy = app.height/2 app.r = 40 def mousePressed(app, event): app.cx = event.x app.cy = event.y def redrawAll(app, canvas): canvas.create_text(app.width/2, 20, text='Move dot with mouse presses', fill='black') canvas.create_oval(app.cx-app.r, app.cy-app.r, app.cx+app.r, app.cy+app.r, fill='darkGreen') runApp(width=400, height=400)
- Moving a Dot with a Timer
from cmu_112_graphics import * def appStarted(app): app.cx = app.width/2 app.cy = app.height/2 app.r = 40 def timerFired(app): app.cx -= 10 if (app.cx + app.r <= 0): app.cx = app.width + app.r def redrawAll(app, canvas): canvas.create_text(app.width/2, 20, text='Watch the dot move!', fill='black') canvas.create_oval(app.cx-app.r, app.cy-app.r, app.cx+app.r, app.cy+app.r, fill='darkGreen') runApp(width=400, height=400)
- Pausing with a Timer
Pausing and stepping are super helpful when debugging animations!from cmu_112_graphics import * def appStarted(app): app.cx = app.width/2 app.cy = app.height/2 app.r = 40 app.paused = False def timerFired(app): if (not app.paused): doStep(app) def doStep(app): app.cx -= 10 if (app.cx + app.r <= 0): app.cx = app.width + app.r def keyPressed(app, event): if (event.key == 'p'): app.paused = not app.paused elif (event.key == 's') and app.paused: doStep(app) def redrawAll(app, canvas): canvas.create_text(app.width/2, 20, text='Watch the dot move!', fill='black') canvas.create_text(app.width/2, 40, text='Press p to pause or unpause', fill='black') canvas.create_text(app.width/2, 60, text='Press s to step while paused', fill='black') canvas.create_oval(app.cx-app.r, app.cy-app.r, app.cx+app.r, app.cy+app.r, fill='darkGreen') runApp(width=400, height=400) - MVC Violations
- Cannot change the model while drawing the view
from cmu_112_graphics import * def appStarted(app): app.x = 0 def redrawAll(app, canvas): canvas.create_text(app.width/2, 20, text='This has an MVC Violation!', fill='black') app.x = 10 # This is an MVC Violation! # We cannot change the model from the view (redrawAll) runApp(width=400, height=400)
- Once again, but with a mutable value (such as a list)
# Since this version modifies a mutable value in the model, # the exception does not occur immediately on the line of the change, # but only after redrawAll has entirely finished. from cmu_112_graphics import * def appStarted(app): app.L = [ ] def redrawAll(app, canvas): canvas.create_text(app.width/2, 20, text='This also has an MVC Violation!', fill='black') app.L.append(42) # This is an MVC Violation! # We cannot change the model from the view (redrawAll) runApp(width=400, height=400)
- Cannot change the model while drawing the view