[Tutor] Tkinter debugging [widget.pack() doesn't have a return value]

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Wed Apr 14 21:03:10 EDT 2004


Hi David,

[You replied to an older message from Ara's request about working with
text files.  Your messages have the subject line 'Filtering Text Files',
even though your question has more to do with Tkinter stuff.


Next time, start a new thread by composing a new message to Tutor.  This
is for purely practical reasons.  One reason why it's important is because
our archiver "threads" messages based on subject:

    http://mail.python.org/pipermail/tutor/2004-April/thread.html

and it become much harder to trace or follow a conversation if the subject
line isn't describing the topic of discussion.  Also, some folks who are
working with in Tkinter stuff will pay more attention to your problem.

Ok, let's get to your question.  *grin*]





> Filtering text filesok, so I am an idiot! There wan no ) after the .pack( ).
> Never mind about that. But now it is saying the this "AttributeError:
> 'NoneType' object has no attribute 'select'"
> in the ugly red letters.  What now?!?!?!

Breathe, and DON'T PANIC.


Be careful not to paraphrase the error when asking for help.  There's has
to be more in the error message, because most Python error messages come
with line numbers that will point at where the problem is.


Here's an example of what we like to see in an error message:

###
[dyoo at tesuque dyoo]$ cat foo.py
class SomeClass(object):
    def sayHello(self):
        pass
    def noSelf():
        print "I'm selfless"

foobar!
[dyoo at tesuque dyoo]$
[dyoo at tesuque dyoo]$
[dyoo at tesuque dyoo]$ python foo.py
  File "foo.py", line 7
    foobar!
          ^
SyntaxError: invalid syntax
###

All the lines, starting from "File ..." are important for debugging
purposes.  Notice that the error message here says exactly what line it
thinks might be causing the problem.  That helps because we don't have to
look at the whole program: we can localize our bug hunt to a smaller area.


So when you see an error message in your own program, make sure to record
that line number and look at the line, and make sure to send the whole bug
error message to Tutor, so that we can follow along.



Going back to the error message you sent,

> "AttributeError:'NoneType' object has no attribute 'select'"

>From the error message, we can see that it has to do with something with
'select'.  So we can do a search through the program and see if we can
find it.  There are a few places where a "select" is being looked up, near
the bottom of the program:

>   if C.select():
>       C=Tkinter.Radiobutton(text="Change dir to C:",
> value=1,command=os.chdir('C:\\').pack()
>   elif D.select():
>       D=Tkinter.Radiobutton(text="Change dir to D:",
> value=1,command=os.chdir('D:\\').pack()
>   elif E.select():
>       E=Tkinter.Radiobutton(text="Change dir to D:",
> value=1,command=os.chdir('E:\\').pack()
>   elif F.select():
>       F=Tkinter.Radiobutton(text="Change dir to D:",
> value=1,command=os.chdir('F:\\').pack()
>   elif G.select():
>       G=Tkinter.Radiobutton(text="Change dir to D:",
> value=1,command=os.chdir('G:\\').pack()


And that's why the line number is important: that error message could
apply to any one of these, so it would have helped to isolate the error a
little further.



Ah, ok, I think I see the problem.  The pack() method of a widget actually
doesn't have a return value --- so something like:

    G = Tkinter.Radiobutton(text="Change dir to D:",
                            value=1,command=os.chdir('G:\\').pack()


actually needs to be broken down into two statements:

    G = Tkinter.Radiobutton(text="Change dir to D:",
                            value=1,command=os.chdir('G:\\')
    G.pack()


This is probably the reason why C, D, E, F, and G are showing up as None.



To make life a little easier, we can write a quicky helper function to do
the work of constructing and packing a radiobutton:

###
def makeRadiobutton(text, value):
    newButton = Tkinter.Radiobutton(text = text, value = value)
    newButton.pack()
    return newButton
###



This should allow you to make radiobuttons without having to worry about
packing them separately:

###
C = makeRadiobutton(text="Change dir to C:", value=0)
D = makeRadiobutton(text="Change dir to D:", value=0)
...
###



Finally, the last statement in the program:

>   Tkinter.mainloop

needs parentheses, or else it won't fire off:

    Tkinter.mainloop()



Good luck to you.




More information about the Tutor mailing list