[Tutor] How to learn to read error messages and generalize from examples [Was Re: Do I have to initialize TKInter before I can use it?]

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Tue Nov 15 06:23:33 CET 2005



On Mon, 14 Nov 2005, Nathan Pinno wrote:

> Alan and all,
>
> I imported it, but its not there. I tried using the Message one found in it,
> but I got an error message:
> >>> import Tkinter
> >>> tk = Tkinter.Tk()
> >>> Tkinter.Message("Help","Help!")


Hi Nathan,

According to the traceback:

> Traceback (most recent call last):
>   File "<pyshell#6>", line 1, in -toplevel-
>     Tkinter.Message("Help","Help!")
>   File "D:\Python24\lib\lib-tk\Tkinter.py", line 2640, in __init__
>     Widget.__init__(self, master, 'message', cnf, kw)
>   File "D:\Python24\lib\lib-tk\Tkinter.py", line 1862, in __init__
>     BaseWidget._setup(self, master, cnf)
>   File "D:\Python24\lib\lib-tk\Tkinter.py", line 1840, in _setup
>     self.tk = master.tk
> AttributeError: 'str' object has no attribute 'tk'

our program gets stuck on the line:

    Tkinter.Message("Help","Help!")

If we assume that the rest of the traceback comes from the internals of
Tkinter, and that those parts are fine, let's start off by assuming that
there might be something on our own program's line that causes the bug.
Our assumption might be wrong, but let's see how far this goes --- we have
to start somewhere.

What is that call to Tkinter.Message()?  It's a call to make a Message
widget.  What arguments do we need to pass when we make such a thing?


If we don't know this offhand --- and that's fine; that's what references
are for --- then we can look at an example, like the one at:

   http://www.pythonware.com/library/tkinter/introduction/x59-details.htm

The tutorial example makes a Label widget:

    from Tkinter import *
    root = Tk()
    w = Label(root, text="Hello, world!")


We see that a Label widget needs the root window object as well as
whatever configuration options we pass to customize that Label widget.


Going back to our buggy code:

    Tkinter.Message("Help","Help!")

can we think of what you might do to make this similar to the example in
the tutorial?  Where's the root object?  What option are we trying to
customize?  Labels and Messages are similar enough that we can make
analogies from the tutorial example to our program.


You may find the detail pages for Labels and Messages useful:

   http://www.pythonware.com/library/tkinter/introduction/x5258-options.htm
   http://www.pythonware.com/library/tkinter/introduction/x6315-options.htm


Good luck to you!



More information about the Tutor mailing list