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

Questions and remarks on Python for Kids

19 May, 2014

Marcin S writes:

I am currently testing your book on my girlfriend who is not a kid anymore but a person that has not written any code in her entire life. To assist her better I have read the whole book. I think I spent my money well when I bought it.

Still, I have some questions and remarks.

Did you consider to emphasize more we are using Python 3 in the entire book? I know that the reader is instructed to download that version, but if someone uses Python 2 for some reason (e.g. computer science classroom with outdated software), one will get different results. In the context of your book I have found two important cases: 1) division always produces a float in Python3 because we no longer have a strict difference between integers and floats, 2) input() works like raw_input() from Python2, while input() in Python2 made implicit eval(). [1]

The errata mentions input() behaves differently than readline() depending on the context -- console or shell. I am unable to replicate it. BTW, the parameter to read()/readline() specifies the maximum number of characters to read, not the exact number. And there is one more thing -- if one reads less characters than available on stdin, remaining characters will be read on the next call to read()/readline(). [2]

Are strings enclosed in triple double-quotes omitted as reserved for documentation? I think only triple single-quotes are mentioned in the book. [3]

Why did you choose to name classes with plural names (class Giraffes) instead of singular (class Giraffe)? [4]

The "is" keyword is mentioned in the keywords section as a complicated concept and is omitted in the examples. I have one remark on that: PEP8 suggests using "is" when checking if something is None (not with "=="). [5]

I have seen range() with 3 arguments mentioned later in the book. Did you consider mentioning slices ([a:b:c]) as well? [6]

Did you consider mentioning "for" and "while" with the optional "else" block? [6]

The book mentions random.choice() but the bounce game uses shuffle() and picking by index instead. [7]

Mr. Stick Man checks for collisions using comparisons glued with "and". Did you consider constructs like "x1 < x < x2" instead of "x1 < x and x < x2"? [8]

Why did you choose a Tk construct based on sequence: update_idletasks(), update(), sleep() instead of mainloop() with rescheduling with Tk.after()? There would not be any active wait then and it would be event-driven. Is not the update_idletasks() included in update()? [9]

I've annotated your comments so I can address each in turn, but the answer to many of your points is somewhat similar:

[1] No, at the time of writing, I didn't consider more emphasis was needed, other than what is detailed in the first chapter. But as time has gone on, the differences between 2 and 3 account for a significantly large percentage of the questions I get about the book, so perhaps something additional makes sense. [2] The problem is reproducible in Python3.2 (and older versions). In 3.3 onwards, the max characters parameter works as expected. [3] No, the triple double-quotes were omitted essentially for simplification reasons. To make it less confusing for younger readers. [4] I chose plural class names mainly to try to indicate class as a kind of grouping - trying to distinguish between that and singular object instances. Yes, not particularly standard, but I had a (possibly vain) hope it would help to highlight the difference between class and object. [5] "is" was omitted again for simplification reasons -- to avoid a complicated description of how object identity works. [6] Again, slices and else blocks in for/while loops were omitted for simplification. [7] You'll find a description of the shuffle function on page 136. [8] Another one omitted to simplify things. Combining conditions with and and or is, I believe, more straightforward for kids to understand (even if x < y < z is a bit cleaner) [9] This one came up in the review from one of the technical proof readers. mainloop is definitely the better (read as correct) approach, but I wanted to stick with a loop and update/update_idletasks because it made it obvious, to the reader, what is happening. Each loop tells one of the objects to do something (move and draw), and then triggers Tk to redraw the screen (the reason for using both update and update_idletasks is that when writing the book -- with an earlier version of Python3 -- I saw some animation jittering when using only the one call. Using both seemed to resolve that issue. Perhaps I should've checked why at the time, and maybe it's no longer an problem with Python3.4)

Hope that helps clarify some of your questions...

Intermediate Python Resources

18 May, 2014

Jim G writes:

I'm currently going through your book python for kids. I was wondering what books or other sources you recommend for learning more about python

Well, there are a heap of online tutorials, and dead-tree, printed books to choose from. However, I have an alternate suggestion to further reading.

Once you've learned the basics, why not pick a project? Find something you want to create - whether that's a game, a desktop application, a website or something else - make sure you have the Python documentation bookmarked, and a search engine to hand, and start working on it. You can't beat actually coding for improving your knowledge and ability.

Let's say you decide to write a program to send a message from one computer to another, on your home network. That might lead you to the socket module in the Python modules documentation. Assuming you find that too opaque, you might search for a "python networking tutorial" or perhaps a "simple python socket example" - but either way, the investigation, and putting that information to work in a program of your own will be a much more valuable learning experience than just reading a dry chapter about networking in some resource.

Python on the iPad?

10 Mar, 2014

Beth S writes:

I teach gifted elementary-aged students in Albuquerque, NM. A few of my students and I have been working through your book. We have limited desktops in our classroom, but we also have access to several iPads. In researching the various apps available, I’ve realized that most (if not all?) do not include the _tkinter module. To be honest, I’m rather new to programming myself!

Do you have a recommendation for an iPad app that would be appropriate for students using your book?

Thank you for your time and your ability to clearly explain the basics of Python!

I'm afraid I don't know of any way to use an iPad for Python, other than by jail-breaking the device (and even then I have no idea whether tkinter would work anyway - I think unlikely).

The only thought I had was whether you could use the desktop PCs in your class to run multiple VMs (using something like VirtualBox) and then connect to those using a Remote Desktop App for the iPad. Not straightforward to set up, but it might accomplish what you want (assuming the desktops are reasonably powerful of course).

Sorry I can't be much help there.

Update: since sending my initial reply, I came across this discussion, which explains why you're not likely to see tkinter on the iPad anytime soon.