Tkinter colorchooser problems

01 May, 2013

Michal writes:

My son has found a problem. It is regarding:

from tkinter import *
colorchooser.askcolor()

It is not working and I cannot help him. On the python shell he gets an error:

Traceback (most recent call last):
File "colorrect.py", line 2, in <module>
colorchooser.askcolor()
NameError: name 'colorchooser' is not defined

Could you help please.

This is an odd one. If I try the code in the Shell, the color chooser is available as expected:

colorchooser1

But if I try the same code in the command-line console, I get a similar error:

colorchooser2

The only way I can reproduce the problem in the Shell is when I don't run it in "No Subprocess" mode - that's if you don't follow the installation instructions, in Chapter 1 of the book, and just run the Shell/IDLE as it was installed (without modifying the shortcut):

colorchooser3

You can get around this, by directly importing the colorchooser module like so:

>>> import tkinter.colorchooser
>>> tkinter.colorchooser.askcolor()

But you'll probably hit problems if you try to call the askcolor function (the second line in that snippet of code above).

All of which is rather messy and confusing. My suggestion would be to make sure you see the "No Subprocess" message when you run IDLE, and if not, revisit Chapter 1. If you do see the message on startup, and still get the error (which I haven't managed to do, but I'm guessing might be possible), try the sample code above, and see if that works for you. Good luck!