Epic Amazon Fail

22 Mar, 2017

Frankly this is bizarre, but if you're looking for Python for Kids, I suggest you don't buy on Amazon for the moment:

Expensive!! (small image)

Much as I'd like to be getting a cut of that price, I somehow doubt I'm getting anything -- and you're possibly getting a poor quality knock off, going by this:


(Some more discussion on Hacker News here: https://news.ycombinator.com/item?id=13924546 )

Barnes and Noble looks to be a safer option - and you can still buy directly from No Starch Press of course.

Further update: Inc.com "Amazon's Tepid Response to Counterfeiters Frustrates Sellers"


18 Jan, 2017

Isai writes:

I am having trouble with this code in page 176:

from tkinter import * colorchooser.askcolor()

When I load it into IDLE, I get a blank canvas, and I can't close it, I have to restart the shell.

I have installed the proper ActiveTcl version (8.5), and no longer get the message giving me errors on startup.

When I run the previous code I was playing with (page 174), the canvas pops up and it all works again. Weirdly enough, the color picker pops up unprompted! I noticed that it does not bring up any buttons that appear in your book: [OK] [Cancel].

That's a bit of a gap in the book, because it doesn't make it clear that you need to create an instance of the top-level Tk widget first, before running that particular example (using tk = Tk()). So the first time you run this function: colorchooser.askcolor(), it's hanging, waiting for something that isn't there yet. Then you run the code on the previous page (which does create the Tk object), and that's when you suddenly get a popup dialog with the colorchooser in it.

So the code should really look like this:

from tkinter import *
from tkinter import colorchooser
tk = Tk()

When you run that, you should then get the colorchooser dialog immediately.

Update #1: One step I missed, is a call to tk.update(), which forces tk to update the screen. Once that's executed, the chooser will appear. The example is updated above.

Update #2: On Windows you need to explicitly import colorchooser for the code to work properly (example updated again).

Pro Alternatives

31 Dec, 2016

After something like 10 or more years completely ensconced in the Apple ecosystem, and now finding the price tag on the new MacBook Pros hard to justify, I'm looking around at alternatives. Before Apple, there was 7 - 8 (only occasionally fustrating) years of Linux in various flavours, so it makes sense to investigate linux-on-a-laptop options again. Looking at the Dell offerings though, it's clear they could learn from Apple's example -- at least in terms of pricing and consistency.

To start with, a higher spec'd MacBook Pro, from the US website:

Apple US (small image)

So that's £2,267 (at time of writing) for a 15" screen, i7 processor, 16GB of RAM and a 512GB solid state disk.

And for comparison, here's the same thing from the Apple UK and Apple NZ stores:

Apple UK (small image) Apple NZ (small image)

The NZ price is roughly £2,637. Apart from the fact that the US price is a few hundred pounds cheaper, they're roughly consistent (in both price, and what you get for your money). VAT (in the UK) and GST (in NZ) probably account for, at least part of, the price variation.

However, compare this with Dell's Precision series laptops. The Precision 15 5000 series (again with 15" screen, i7 processor and 512GB SSD) installed with Ubuntu Linux, comes in at a much more reasonable £1,483:

Dell US (small image)

The advantage of the Dell is that you can get a second 1TB HDD installed and the price is still less than the MacBook Pro (£1,558).

However, on the UK website, you can't order the additional hard drive, but the price is, at least, consistent (even if their website doesn't make it immediately obvious that you can order an i7 processor):

Dell - no HDD

Dell UK (small image)

However, compare with Dell NZ:

Dell NZ (small image)

That's £4,095, give or take a few pence. A price variation I can only see justified, if Dell haven't figured out international shipping, and are flying the laptop into the country in a business class seat. In addition, not only do the websites vary slightly in each country, but the configuration options vary as well. In the US you can get the additional HDD, in the UK and NZ you cannot (perhaps some different regulations applying to the ex-US models, and the additional HDD doesn't fit?). In the UK, you can get a single 1x16GB memory unit installed, in the US and NZ stores you cannot.

Conclusions? Apple: too expensive for a higher spec. Dell: either obscenely expensive, or inconsistent specifications, depending on which country I happen to be in (not to forget that if I want a bit of future proofing, it's difficult to be confident in the options). Other alternatives? A System76 Oryx Pro perhaps?

Update: Dell really is a bit of a mess. The NZ price for the XPS 15 (with ultra high def display) is only about £200 more than the UK price, but once again the configuration options look to be different between countries. Also with the XPS series, you don't have any option but to pay the Windows tax.

Python 3 on Ubuntu 16

10 Dec, 2016

Roxana T writes (excerpted):

I did look at the installation instructions in ch1 but still didn't work. I'm using Ubuntu and the built-in (is this the right word?) Python Version. It is I guess 2.7. I have the 3 version too, but I have to set it as default probably because when I launch Idle I always get Python 2.7.

Since I wrote Python for Kids, Ubuntu has changed their software installer and, at least in Ubuntu 16.04 when I try, I don't see a package installer for Idle (even though there used to be one available). So the best thing to do is to install it from the command line, by opening the Terminal and running:

sudo apt-get install idle3

(You'll need to enter the administrator password). You should see something similar to the following:

Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
  fonts-opensymbol fonts-stix libboost-date-time1.58.0
  libboost-iostreams1.58.0 libclucene-contribs1v5 libclucene-core1v5
  libcmis-0.5-5v5 libcolamd2.9.1 libeot0 libetonyek-0.1-1 libexttextcat-2.0-0
  libexttextcat-data liblangtag-common liblangtag1 libmhash2 libmwaw-0.3-3
  libmythes-1.2-0 libneon27-gnutls libodfgen-0.1-1 liborcus-0.10-0v5
  libraptor2-0 librasqal3 librdf0 librevenge-0.0-0 libsuitesparseconfig4.4.6
  libwps-0.4-4 libyajl2 lp-solve uno-libs3 ure
Use 'apt autoremove' to remove them.
The following additional packages will be installed:
  blt idle-python3.5 python3-tk tk8.6-blt2.5
Suggested packages:
  blt-demo tix python3-tk-dbg
The following NEW packages will be installed
  blt idle-python3.5 idle3 python3-tk tk8.6-blt2.5
0 to upgrade, 5 to newly install, 0 to remove and 1 not to upgrade.
Need to get 647 kB of archives.
After this operation, 2,345 kB of additional disk space will be used.
Do you want to continue? [Y/n]

Hit Y to continue, and after installation, you should be able to run idle3 from the Terminal; and then, you won't get the funny compatibility errors you're been hitting with Python2. I also see Idle for Python 3.5 available from the launcher after installation, so you don't necessarily need to use the Terminal after that, if you don't want to.

idle in the launcher

Text position in tkinter

08 Dec, 2016

Drew M writes:

I have a question I am sure I don't know :) In tkinter I am creating a game and can't figure out how to set dimensions for text. I need it on the middle. My canvas is 1000 by 1000 so I divided in half. Which is of course 500. Now again, my question is how to put it there.

Adding text to a canvas is straightforward (similar to adding any other item, such as a line, or a rectangle). You say "middle" and not "center", so I assume you don't want the text directly in the center of the canvas? I've defined a smaller canvas here, but the principle is the same:

from tkinter import *
tk = Tk()
canvas = Canvas(tk, width=100, height=100, bd=0, highlightthickness=0)
canvas.create_text(50, 50, text='text')

text position center

So if you don't want the center, I'm guessing your problem is that this code...

from tkinter import *
tk = Tk()
canvas = Canvas(tk, width=100, height=100, bd=0, highlightthickness=0)
canvas.create_text(50, 0, text='text')

...(with coordinates of 50, 0) results in partially obscured text:

text position middle

The answer, is to use the named parameter anchor in the create_text function:

canvas.create_text(50, 0, text='text', anchor=N)

The anchor parameter controls the positioning of an item in terms of its coordinates. The default value is CENTER (so using this puts the center of the text at the coordinates (50, 0) in the earlier example), but you can also use NW, N, NE (effectively top-left, top-middle, top-right), W, E (left and right), and SW, S, SE (bottom-left, bottom-middle, bottom-right). So N puts the top and middle point of the text at the coordinates (50, 0), like so:

text position N

I hope that helps.

PfK and Python 2

10 Jul, 2016

Tara P writes:

I am going to be teaching Python 2 this coming school year and I am looking for a supplemental learning tool and projects for myself and my students. Will your book Python for Kids work with Python 2?

I saw that it is recommended to use Python 3 however the curriculum I am teaching we are to use Python 2. I am not familiar enough with Python to know if the code language is different enough between them where your book will not be useful for me?

The short answer is no, it is not really designed to work with Python 2. The longer answer is that most of the code samples will work, but there are subtle differences between the two versions that may make it a little more difficult to get things working properly. There are some obvious differences (such as the tkinter module being called Tkinter in Python 2), which are straightforward to deal with and explain, but the more complicated the code, the more likely you are to hit issues that are less obvious, and as a consequence, cause frustrating errors for your students (I'm thinking more of the games in the later chapters, which aren't tested with Python2, and may either fail or hang).

While moving from Python 2 to 3 is not enormously difficult, it's not necessarily something I would recommend for learners coming to a programming language for the first time.

Python3.5 on macOS

10 Jun, 2016

If you're installing Python3.5 on macOS, you don't need to follow the Automator instructions in Python for Kids any more. After installation, it's as simple as searching for IDLE in Spotlight (the magnifying glass in the top righthand side of the menu bar) and double-clicking on (usually) the top hit ("IDLE - Python 3.5").

You might still get an error message saying something like:

The version of Tcl/Tk (8.5.9) in use may be unstable. Visit http://www.python.org/download/mac/tcltk for current information

Tcl is a simple programming language and Tk is (sort of) a graphics library (more info here http://tldp.org/HOWTO/TclTk-HOWTO-3.html) -- the Tk graphics library is heavily used in later chapters of the book. If you happen to get the above error message, follow the instructions on the python.org website to install the correct version of ActiveTcl for the version of macOS you're running. This post may also be of help.

Python on Chromebook

01 Mar, 2016

Cynthia B writes:

I purchased your book Python for Kids so that my 11 year old daughter and I could start to learn code together. We are trying to install Python on her (lenovo 100S) chromebook to no avail.

We followed several prompts and purchased a python shell which has been installed but can not figure out where to go from here, or if this will even be possible. Any advice?

As far as I'm aware, the Python shell extensions available in the Chrome OS store aren't Python 3 compatible - so at least from a version perspective, that's a non-starter. Even if the version was correct, I suspect your mileage would probably vary with some of the later code examples in the book, using that sort of environment (not a fully fledged installation... again AFAIA).

I don't have a Chromebook to try it out myself, but from a bit of searching, it looks like you can dual-boot Linux, and after that should be able to install anything you like (http://stackoverflow.com/a/15261253/2685350 - although Samsung-specific, I would hope the basic idea still applies).

I've found some instructions on doing so at the following sites: http://www.howtogeek.com/162120/how-to-install-ubuntu-linux-on-your-chromebook-with-crouton/ http://lifehacker.com/how-to-install-linux-on-a-chromebook-and-unlock-its-ful-509039343 http://www.linux.com/learn/tutorials/764181-how-to-install-linux-on-an-acer-c720-chromebook


21 Feb, 2016

Kishore writes:

I bought this great book "python for kids". I want to give it as a gift to kid in India. She doesn't have internet, but has a computer. What do I need to download and give in a pendrive, so that she can finish the book without an internet connection.

Apart from the book itself, there's two extras to download: 1 - the solutions to all the programming challenges at the end of each chapter, and 2 - the code samples in the book (in case she has difficulty getting something to work).

The solutions are available from the publishers website here: http://nostarch.com/pythonforkids. A zip file of all the code can be found here: /python-for-kids/code.zip.

Depending on when you purchased the book, you might also want to take a copy of the eratta. If it's a fairly recent reprint, there's no need. If the printing is before April 2015, then you should probably send that to her as well.

You didn't mention whether she already has Python installed, so if not, I suggest reading the first chapter yourself, so that you can include the right installation files for her OS.

A parameter called evt

26 Jan, 2016

Dudley B writes:

I'm a Business Analyst trying to learn how to code. Luckily there's Python, a suitable language for absolute beginners like me. Thank God also for your book, Python for Kids. It's a great book which explains Python fundamentals in a fun and easy way. In Chapter 14 of the book, page 208, there is a parameter called "evt":

def turn_left(self, evt):     self.x = -2 def turn_right(self, evt):     self.x = 2

I can't figure out what this parameter is for, and why it's there since there is no value passed on to it. Please help me understand why "evt" is needed for those functions.

The evt parameter isn't brilliantly explained in that chapter, now that I look back at it. If you go back to the section titled "Making an object React to something" in Chapter 12, there's a description there of event bindings. The function described takes a parameter called event. After you've "told" tkinter about the function (that's the binding part), when an event occurs (such as the mouse being moved, or keyboard being pressed) it calls that function with an object containing more information (such as what sort of event it was).

The parameter evt on page 208, is simply a shortened name for the parameter event mentioned in the earlier chapter. In fact, the name isn't important at all -- you could even call it bob, or aardvark, or anything else you like.

By the way, another way of thinking about the tkinter event binding is that it's like a contract between two people. Let's say between a surfer, and an old man living on a cliff above the sea. The surfer says to the old man, "send me a message and let me know how high the surf is, whenever it changes". The function is that contract, and the message about the surf is the parameter value.

Hopefully that helps.