[Tutor] Do I have to initialize TKInter before I can use it?

John Fouhy john at fouhy.net
Tue Nov 15 02:54:15 CET 2005


On 15/11/05, Nathan Pinno <falcon3166 at hotmail.com> wrote:
>
>
> John and all,
>
> I am having problems. The latest error message I got was:
> Traceback (most recent call last):
>   File "D:\Python24\hockey.py", line 19, in -toplevel-
>     tkMessageBox.showinfo("Hockey",
> NameError: name 'tkMessageBox' is not defined
>
> What do I have to do in this case?

This means python doesn't know what a 'tkMessageBox' is.

Names get bound to things in python in one of two main ways:

1. You bind the name yourself, usually by using an assignment statement. eg:

>>> x
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
NameError: name 'x' is not defined
>>> x = 3
>>> x
3

2. An import statement brings the name in. eg:

>>> math
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
NameError: name 'math' is not defined
>>> import math
>>> math
<module 'math' (built-in)>

You can also use 'from ... import' to import specific names.

>>> sin
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
NameError: name 'sin' is not defined
>>> from math import sin
>>> sin
<built-in function sin>

In your case, you probably want to bring tkMessageBox in using an
import statement.  I'm guessing you have done 'from Tkinter import *'.
 You can figure out what names that brings in by using dir:

>>> import Tkinter
>>> dir(Tkinter)
['ACTIVE', 'ALL', 'ANCHOR', 'ARC', 'At', 'AtEnd', 'AtInsert',
'AtSelFirst', 'AtSelLast', 'BASELINE', 'BEVEL', 'BOTH', 'BOTTOM',
'BROWSE', 'BUTT', 'BaseWidget',
# etc

If you look through that list, you will see that tkMessageBox is not
there, which means that it is not part of the Tkinter module.  Where
did you find out about tkMessageBox? Have you looked at any
documentation?

--
John.


More information about the Tutor mailing list