[Tutor] what am I missing?
alan.gauld@bt.com
alan.gauld@bt.com
Sat, 1 Sep 2001 17:55:49 +0100
------_=_NextPart_001_01C13306.F3731370
Content-type: text/plain; charset="ISO-8859-1"
from Tkinter import *
from ScrolledText import *
def doSearch(event):
sTerm=StringVar()
This is never used , might as well delete it!
box=Toplevel()
You define box here, inside doSearch, so its not visible
anywhere else in the program. IMHO you are better to
create your GUI el;ements either as members of a class or,
if your not happy with OO yet as global variables
- ie outside of any function.
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)
def quit():
root.quit()
def search():
sTerm=StringVar()
Not sure why you are using StrinGVar() here, you don't
seem to use the 'magical' properties of StringVar,
so a normal string would suffice...
sTerm=box.entry.get() #produces attribute error as below
especially since you now replace the StringVar with a string
- or you would if your function could see box.
display.insert(sTerm)
Similarly this won't work coz display is a name only known
to the doSearch function.
Your problems are not really to do with Tlinter but with your understanding
of Pythons naming rules. Try looking at my chapter on namespaces to see if
that helps:
http://www.crosswinds.net/~agauld <http://www.crosswinds.net/~agauld>
/tutname.htm
root=Tk()
fr=Frame(root)
button=Button(fr)
button['text']= 'Search'
button.bind('<Button-1>', doSearch)
button.pack()
Why not just:
Button(fr, text='Search', command=doSearch).pack()
As you do below...?
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()
So what the program does is create a window with 2 buttons and
a text area. When you press the Sarch button it creates a dialog
with a label, entry, text area and button.
When you hit the dialog button it calls search which has no
knowledge of the dialog you created only the original
window(which is global in scope. But Search tries to
read the value in the dialog. You either need to pass the
entry and display widgets to the search fnction or make
them globally available.
HTH,
Alan G
------_=_NextPart_001_01C13306.F3731370
Content-type: text/html; charset="ISO-8859-1"
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=ISO-8859-1">
<META content="MSHTML 5.00.3013.2600" name=GENERATOR></HEAD>
<BODY bgColor=#ffffff style="FONT-FAMILY: Arial" text=#000000>
<BLOCKQUOTE
style="BORDER-LEFT: #0000ff 2px solid; MARGIN-LEFT: 5px; MARGIN-RIGHT: 0px; PADDING-LEFT: 5px">
<DIV>from Tkinter import *<BR>from ScrolledText import *</DIV>
<DIV> </DIV></BLOCKQUOTE>
<DIV style="MARGIN-RIGHT: 0px">def doSearch(event):<BR>
sTerm=StringVar()<SPAN class=910503716-01092001><FONT color=#0000ff
face="Courier New" size=2> </FONT></SPAN></DIV>
<DIV style="MARGIN-RIGHT: 0px"><SPAN class=910503716-01092001><FONT
color=#0000ff face="Courier New" size=2>This is never used</FONT> <FONT
color=#0000ff face="Courier New" size=2>, might as well delete
it!</FONT></SPAN></DIV>
<DIV style="MARGIN-RIGHT: 0px"><SPAN
class=910503716-01092001></SPAN><BR>
box=Toplevel()<BR> <SPAN class=910503716-01092001> <FONT
color=#0000ff face="Courier New" size=2> </FONT></SPAN></DIV>
<DIV style="MARGIN-RIGHT: 0px"><SPAN class=910503716-01092001></SPAN><FONT
size=2><FONT color=#0000ff><FONT face="Courier New"><SPAN
class=910503716-01092001>You define box here, inside doSearch, so its not
visible </SPAN></FONT></FONT></FONT></DIV>
<DIV style="MARGIN-RIGHT: 0px"><FONT size=2><FONT color=#0000ff><FONT
face="Courier New"><SPAN class=910503716-01092001>anywhere else in the
program. IMHO you are better to </SPAN></FONT></FONT></FONT></DIV>
<DIV style="MARGIN-RIGHT: 0px"><FONT size=2><FONT color=#0000ff><FONT
face="Courier New"><SPAN class=910503716-01092001>create your GUI el;ements
either as members of a class or, </SPAN></FONT></FONT></FONT></DIV>
<DIV style="MARGIN-RIGHT: 0px"><FONT size=2><FONT color=#0000ff><FONT
face="Courier New"><SPAN class=910503716-01092001>if your not happy with OO yet
as global variables </SPAN></FONT></FONT></FONT></DIV>
<DIV style="MARGIN-RIGHT: 0px"><FONT size=2><FONT color=#0000ff><FONT
face="Courier New"><SPAN class=910503716-01092001>- ie outside of any
function.</SPAN></FONT></FONT></FONT></DIV>
<DIV style="MARGIN-RIGHT: 0px"><FONT size=2><FONT color=#0000ff><FONT
face="Courier New"><SPAN
class=910503716-01092001></SPAN></FONT></FONT></FONT> </DIV>
<DIV style="MARGIN-RIGHT: 0px"> label=Label(box, text='Enter
term').pack()<BR> entry=Entry(box)<BR>
Button(box, text='Look For', command=search).pack()<SPAN
class=910503716-01092001><FONT color=#0000ff face="Courier New"
size=2> </FONT></SPAN></DIV>
<DIV style="MARGIN-RIGHT: 0px">
entry.pack()<BR> entry.focus()<BR>
display=ScrolledText(box, width=40, height=20, bg='white').pack(expand=YES,
fill=BOTH)<BR> <BR>def quit():<BR>
root.quit()</DIV>
<BLOCKQUOTE
style="BORDER-LEFT: #0000ff 2px solid; MARGIN-LEFT: 5px; MARGIN-RIGHT: 0px; PADDING-LEFT: 5px">
<DIV>def search():<BR> sTerm=StringVar()<SPAN
class=910503716-01092001><FONT color=#0000ff face="Courier New"
size=2> </FONT></SPAN></DIV></BLOCKQUOTE>
<DIV><FONT color=#0000ff face="Courier New" size=2><SPAN
class=910503716-01092001>Not sure why you are using StrinGVar() here, you don't
</SPAN></FONT></DIV>
<DIV><FONT color=#0000ff face="Courier New" size=2><SPAN
class=910503716-01092001></SPAN></FONT><FONT color=#0000ff face="Courier New"
size=2><SPAN class=910503716-01092001>seem to use the 'magical' properties of
StringVar, </SPAN></FONT></DIV>
<DIV><FONT color=#0000ff face="Courier New" size=2><SPAN
class=910503716-01092001></SPAN></FONT><FONT color=#0000ff face="Courier New"
size=2><SPAN class=910503716-01092001>so a normal string would
suffice...</SPAN></FONT></DIV>
<DIV><FONT color=#0000ff face="Courier New" size=2><SPAN
class=910503716-01092001></SPAN></FONT><BR>
sTerm=box.entry.get() #produces attribute error as below<SPAN
class=910503716-01092001><FONT color=#0000ff face="Courier New"
size=2> </FONT></SPAN></DIV>
<DIV><SPAN class=910503716-01092001></SPAN> </DIV>
<DIV><FONT color=#0000ff face="Courier New" size=2><SPAN
class=910503716-01092001>especially since you now replace the StringVar with a
string</SPAN></FONT></DIV>
<DIV><FONT color=#0000ff face="Courier New" size=2><SPAN
class=910503716-01092001>- or you would if your function could see
box.</SPAN></FONT></DIV>
<DIV><SPAN class=910503716-01092001> </SPAN><BR>
display.insert(sTerm)<BR><SPAN class=910503716-01092001><FONT color=#0000ff
face="Courier New" size=2></FONT></SPAN></DIV>
<DIV><SPAN class=910503716-01092001><FONT color=#0000ff face="Courier New"
size=2>Similarly this won't work coz display is a name only known
</FONT></SPAN></DIV>
<DIV><SPAN class=910503716-01092001><FONT color=#0000ff face="Courier New"
size=2>to the doSearch function.</FONT> </SPAN></DIV>
<DIV><SPAN class=910503716-01092001></SPAN> </DIV>
<DIV><FONT color=#0000ff face="Courier New" size=2><SPAN
class=910503716-01092001>Your problems are not really to do with Tlinter but
with your understanding of Pythons naming rules. Try looking at my chapter on
namespaces to see if that helps:</SPAN></FONT></DIV>
<DIV><FONT color=#0000ff face="Courier New" size=2><SPAN
class=910503716-01092001></SPAN></FONT> </DIV>
<DIV><FONT color=#0000ff face="Courier New" size=2><SPAN
class=910503716-01092001><A
href="http://www.crosswinds.net/~agauld">http://www.crosswinds.net/~agauld</A>/tutname.htm</SPAN></FONT></DIV>
<DIV><FONT color=#0000ff face="Courier New" size=2><SPAN
class=910503716-01092001></SPAN></FONT> </DIV>
<BLOCKQUOTE
style="BORDER-LEFT: #0000ff 2px solid; MARGIN-LEFT: 5px; MARGIN-RIGHT: 0px; PADDING-LEFT: 5px">
<DIV><SPAN
class=910503716-01092001> </SPAN>root=Tk()<BR>fr=Frame(root)<BR>button=Button(fr)<BR>button['text']=
'Search'<BR>button.bind('<Button-1>', doSearch)<BR>button.pack()</DIV>
<DIV> </DIV></BLOCKQUOTE>
<DIV><FONT color=#0000ff face="Courier New" size=2><SPAN
class=910503716-01092001>Why not just:</SPAN></FONT></DIV>
<DIV><FONT color=#0000ff face="Courier New" size=2><SPAN
class=910503716-01092001></SPAN></FONT> </DIV>
<DIV><FONT color=#0000ff face="Courier New" size=2><SPAN
class=910503716-01092001>Button(fr, text='Search',
command=doSearch).pack()</SPAN></FONT></DIV>
<DIV><FONT color=#0000ff face="Courier New" size=2><SPAN
class=910503716-01092001></SPAN></FONT> </DIV>
<DIV><FONT color=#0000ff face="Courier New" size=2><SPAN
class=910503716-01092001>As you do below...?</SPAN></FONT></DIV>
<BLOCKQUOTE
style="BORDER-LEFT: #0000ff 2px solid; MARGIN-LEFT: 5px; MARGIN-RIGHT: 0px; PADDING-LEFT: 5px">
<DIV> </DIV>
<DIV>lbox=ScrolledText(fr, height=20, width=40, bg='white').pack(expand=YES,
fill=BOTH)<BR>#root.insert(END, 'Word') # doesn't work<BR>Button(fr,
text='QUIT', command=quit).pack()<BR>fr.pack()</DIV>
<DIV> </DIV>
<DIV>root.mainloop()</DIV>
<DIV> </DIV></BLOCKQUOTE>
<DIV><FONT color=#0000ff face="Courier New" size=2><SPAN
class=910503716-01092001>So what the program does is create a window with 2
buttons and </SPAN></FONT></DIV>
<DIV><FONT color=#0000ff face="Courier New" size=2><SPAN
class=910503716-01092001>a text area. When you press the Sarch button it creates
a dialog</SPAN></FONT></DIV>
<DIV><FONT color=#0000ff face="Courier New" size=2><SPAN
class=910503716-01092001>with a label, entry, text area and
button.</SPAN></FONT></DIV>
<DIV><FONT color=#0000ff face="Courier New" size=2><SPAN
class=910503716-01092001></SPAN></FONT> </DIV>
<DIV><FONT color=#0000ff face="Courier New" size=2><SPAN
class=910503716-01092001>When you hit the dialog button it calls search which
has no </SPAN></FONT></DIV>
<DIV><FONT color=#0000ff face="Courier New" size=2><SPAN
class=910503716-01092001>knowledge </SPAN></FONT><FONT color=#0000ff
face="Courier New" size=2><SPAN class=910503716-01092001>of the dialog you
created only the original </SPAN></FONT></DIV>
<DIV><FONT color=#0000ff face="Courier New" size=2><SPAN
class=910503716-01092001>window(which is </SPAN></FONT><FONT color=#0000ff
face="Courier New" size=2><SPAN class=910503716-01092001>global in scope. But
Search tries to </SPAN></FONT></DIV>
<DIV><FONT color=#0000ff face="Courier New" size=2><SPAN
class=910503716-01092001>read the value in the </SPAN></FONT><FONT color=#0000ff
face="Courier New" size=2><SPAN class=910503716-01092001>dialog. You either need
to pass the </SPAN></FONT></DIV>
<DIV><FONT color=#0000ff face="Courier New" size=2><SPAN
class=910503716-01092001>entry and display widgets </SPAN></FONT><FONT
color=#0000ff face="Courier New" size=2><SPAN class=910503716-01092001>to the
search fnction or make </SPAN></FONT></DIV>
<DIV><FONT color=#0000ff face="Courier New" size=2><SPAN
class=910503716-01092001>them globally available.</SPAN></FONT></DIV>
<DIV><FONT color=#0000ff face="Courier New" size=2><SPAN
class=910503716-01092001></SPAN></FONT> </DIV>
<DIV><FONT color=#0000ff face="Courier New" size=2><SPAN
class=910503716-01092001>HTH,</SPAN></FONT></DIV>
<DIV><FONT color=#0000ff face="Courier New" size=2><SPAN
class=910503716-01092001></SPAN></FONT> </DIV>
<DIV><FONT color=#0000ff face="Courier New" size=2><SPAN
class=910503716-01092001>Alan G</SPAN></FONT></DIV></BODY></HTML>
------_=_NextPart_001_01C13306.F3731370--