18 Jan, 2017

Isai writes:

I am having trouble with this code in page 176:

from tkinter import *

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 *
tk = Tk()

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

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

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.