Is The Animation Loop Necessary?

17 Aug, 2014

Christopher D writes:

I'm currently working through your book and enjoying it immensely. The following is baffling me though, so hoping you can help.

In Chapter 13, page 197 (after creating the ball object), you wrote: "If you run this program now using Run>Module, the canvas will appear for a split second and then vanish". You then go on to write about how its necessary to add the animation loop to stop this happening.

My window appears and draws the ball correctly, but does not vanish. Reading through the code I don't understand why it's expected to disappear either. This seems to be a key point so would be grateful if you could shed light! I'm running Python 3.4.1 by the way.

Thank you in advance for your help!

You've found an error in the book, I'm afraid. If you run the code using the Python console (try it by opening a command prompt, or terminal, and running python bounce.py), the window will indeed appear and vanish. The basic idea is, there's nothing left for it to do, so no reason for the window to remain open. That's the reason for adding an animation loop of some kind (in the book I use a very basic while-loop, but tkinter also has a mainloop function for doing this). However, when you run the same code with Idle (the Python Shell), which is written using tkinter, the window remains open because there's already a main loop running (started by the Shell itself). So the book is both right and wrong, I guess, depending on how you want to run the code. The errata has been updated accordingly. Thanks for the "bug report".


Trouble with Automator

27 Jul, 2014

Jean-Jacques B writes:

I am trying to set up my iMac for the book, having trouble with the automator script:

open -a "Applications/Python 3.2/IDLE.app" --args -n

Run Shell Script failed open: unrecognized option '--args'

I can get python to launch the other ways, but then turtle doesn't work.

This had me stumped for a moment (couldn't find any similar errors when I googled).

It actually looks like a poorly handled error in the Automator. If you copy+paste what you entered into the Terminal, you get an entirely different error message:

$ open -a "Applications/Python 3.2/IDLE.app" --args -n  
Unable to find application named 'Applications/Python 3.2/IDLE.app'

Which hopefully helps you figure out where you went wrong... try adding a / (slash) in front of the path to IDLE. eg.:

open -a "/Applications/Python 3.2/IDLE.app" --args -n

Python 3.4 Shell

15 Jun, 2014

Kirk K writes:

On page 12 it says File>New Window but I cannot find it on our Windows version 3.4.1. Please advise.

It looks like, as of Python 3.4, they've changed the menu, so File->New Window has become File->New File (which makes the Shell consistent with other text editors, at least). It still has the same behaviour though - opening a new window for typing in your Python code.


Restart when running tkinter

13 Jun, 2014

Craig S writes:

My son is having a problem running the random rectangle program on page 174 of Python for Kids. He is using version 2.7.7, so the Tkinter module must be capitalized (just so you don't suggest to capitalize it). Every time he tries to run the module, it comes up with a new window saying, "RESTART". He tried to copy and paste your code, but came up with the same results. This is the code he's written.
from Tkinter import *
import random
tk = Tk()
canvas = Canvas(tk, width=400,height=400)
def random_rectangle(width, height):
    ...
random_rectangle(400, 400)

This is what comes up when he runs it.
Python 2.7.7 (default, Jun 1 2014, 14:17:13) on win32
Type "copyright", "credits" or "license()" for more information.
>>> =============== RESTART ===============

Please share some advice. Thanks.

A couple of thoughts:

  1. He's missing the pack function after the 4th line of that code (canvas.pack())
  2. You're probably not running Idle in "No Subprocess" mode (see Chapter 1 of the book for setup instructions). If that doesn't work for you, for some reason, then best thing to do is use a text editor, save the code and then run it from the command prompt by typing python yourscript.py (see http://windows.microsoft.com/en-us/windows/command-prompt-faq for instructions on running the command prompt if you're not aware how to do that)
  3. You may hit other weird issues if you're not using Python3, since all the code in the book was written and tested with 3.x. Much of it is probably fine, but no guarantees there...

Which format?

12 Jun, 2014

Koki Y writes:

I have a question. You use % for format instead .format(). In the python tutorial, it is written that % format is an older version of format. What's the point of using % format?

The book uses the % operator (also known as the interpolation operator) basically through omission more than anything else. The first electronic version was written for Python2 (back in 2006/2007), then updated to be compatible with both 2 and 3. While it was almost totally rewritten, to be published as "Python for Kids", some of the existing text was repurposed -- however, the string formatting section wasn't specifically updated for Python 3. I suspect that's somehing we should probably fix when/if it's reprinted again.

By all means, use the format function as detailed in the Python documentation.