[Tutor] Raiobutton question

Danny Yoo dyoo@hkn.eecs.berkeley.edu
Sun, 11 Feb 2001 04:09:55 -0800 (PST)

On Sun, 11 Feb 2001, Glen Wheeler wrote:

> var = IntVar()
[some code omitted]
> root = Tk()

>   I don't know why I get this error
> Traceback (most recent call last):
> AttributeError: 'None' object has no attribute 'tk'

>   It is almost verbatim from J Grayson's book.  

The problem is that when we construct an InvVar(), Tk expects us to have a
root window already set up.  The line:

    root = Tkinter.Tk()

initializes _tk to something that all other widgets will depend
on.  So, when we do something like:

root = Tkinter.Tk()
button = Tkinter.Button()

in the background, the Button() knows that it should connect up with root,
because the _tk variable's initialized.

Going back to your program, the IntVar definition is a bit too early;
IntVar doesn't know where to attach to until we either make a root window.  
Try putting it after your Tk() call, and you should be ok.

> Also, I'd like to know how to fix errors such as these if they come up
> in the future - this is the first time I have seen one like this.

To tell the truth, I have no clue what an InvVar is.  But if you ever see
something that says "_Tk is undefined", it's probably because a Tk()
instance hasn't been built yet.

(Someday, I shall have to take a look at "Python and Tkinter Programming",
and really understand what's happening... *grin*)

Good luck!