Co-existence

28 Aug, 2014

Marta C writes:

I'm new in programming. I just want to know if it would be too hard or complicated to use Python 2.7 with the Python for Kids book? I am still practicing for a class I took with v 2.7. Can I have both IDLE 2.7 and 3 in the system and use them separately and not run into problems? Thank you.

Most of the examples in the book will work with Python 2.7 -- but some won't without modification (particularly anything using the tkinter module). And you may hit subtle problems which will be harder to figure out as a consequence. However, you can install both Python 2.7 and 3 on the same system (you didn't say, but I'm guessing you're using Windows -- after installation, you'll find you've got both c:\Python27 and c:\Python34 directories, assuming you install the latest Python version, on your hard drive) -- just make sure you select the correct directory when setting up the IDLE shortcut (described in Chapter 1).


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...