[Tutor] what am I missing?

Sheila King sheila@thinkspot.net
Fri, 31 Aug 2001 23:27:16 -0700


I really don't know as I care for just throwing in a "global"
everywhere, to solve this scoping problem. Sometimes you can't do it any
other way, but this one could be solved in other ways (it seems to me).

To William:
The problem you were having with the error message about the "global
box", was that your code first mentions this statement:

box = Toplevel() 

inside the doSearch function. Because you mention it inside that
function, then (barring use of the "global" keyword), the other parts of
your code can't see it. They can't see inside of the other functions to
see what they are doing. So when your search() function tries to do
something to the "box", it gets confused, because it doesn't know where
it came from. Putting the phrase "global box" in there, tells the
function to look outside itself, to a "box" object that all parts of the
program are allowed to access.

Ignacio has shown you one possible solution. By declaring the identifier
"box" to be a global, this means ALL parts of your code have access to
it and can see it.

Other possibilities:
Declare the box inside your main code, but create it hidden, and pass it
as a parameter to the functions.

OR:
Write a class, that is a box object, that subclasses a Toplevel, and
give it its own "search" method functions. The book Programming Python
shows a lot about how to effectively use classes with Tkinter objects
and subclass them. I've found it to be a really neat approach to the
problem.

--
Sheila King
http://www.thinkspot.net/sheila/
http://www.k12groups.org/



On Sat, 1 Sep 2001 02:07:00 -0400 (EDT), Ignacio Vazquez-Abrams
<ignacio@openservices.net>  wrote about Re: [Tutor] what am I missing?:

:On Sat, 1 Sep 2001, William Perry wrote:
:
:Whoops.
:
:> from Tkinter import *
:> from ScrolledText import *
:
:box=None
:
:> def doSearch(event):
:
:    global box
:
:>     sTerm=StringVar()
:>     #print 'HERE'
:>     box=Toplevel()
:>
:>     label=Label(box, text='Enter term').pack()
:>     entry=Entry(box)
:>     Button(box, text='Look For', command=search).pack()
:>     entry.pack()
:>     entry.focus()
:>     display=ScrolledText(box, width=40, height=20, bg='white').pack(expand=YES, fill=BOTH)
:>
:>
:>
:>     #sTerm=entry.get
:>     #search(sTerm)
:>     #print sTerm
:>
:> def quit():
:>     root.quit()
:>
:> def search():
:
:    global box
:
:>     sTerm=StringVar()
:>     sTerm=box.entry.get()  #produces attribute error as below
:>     print sTerm
:>     #root.insert(END, sTerm)   ##############
:>     #lbox.insert(END, sTerm)   # doesn't work
:>     #fr.lbox.insert(END, sTerm) ################
:>     display.insert(sTerm)
:>     #pass
:>
:>
:>
:>
:>
:> root=Tk()
:> fr=Frame(root)
:> button=Button(fr)
:> button['text']= 'Search'
:> button.bind('<Button-1>', doSearch)
:> button.pack()
:>
:> lbox=ScrolledText(fr, height=20, width=40, bg='white').pack(expand=YES, fill=BOTH)
:> #root.insert(END, 'Word') # doesn't work
:> Button(fr, text='QUIT', command=quit).pack()
:> fr.pack()
:>
:> root.mainloop()
:>
:>
:> Error produced when the ' Look For ' button is pressed
:>
:> Exception in Tkinter callback
:> Traceback (most recent call last):
:>   File "c:\python21\lib\lib-tk\Tkinter.py", line 1285, in __call__
:>     return apply(self.func, args)
:>   File "C:\WINDOWS\Desktop\Programing\getdata.py", line 31, in search
:>     sTerm=box.entry.get()
:> NameError: global name 'box' is not defined
:>
:>
:> Thanks..
:> Bill Perry
:>
:>