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

Nathan Pinno falcon3166 at hotmail.com
Tue Nov 15 04:48:56 CET 2005


John,

I learned it from the docs on pythonware.com - the introduction to TKInter -
Standard Dialogs.
Since it doesn't exist, how can I show a info box before the main part of my
program?

Nathan 
Date: Tue, 15 Nov 2005 14:54:15 +1300
From: John Fouhy <john at fouhy.net>
Subject: Re: [Tutor] Do I have to initialize TKInter before I can use
	it?
To: Tutor <tutor at python.org>
Message-ID: <5e58f2e40511141754r80505dfg at mail.gmail.com>
Content-Type: text/plain; charset=ISO-8859-1

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