[Tutor] what am I missing?
William Perry
wmperry@swbell.net
Sat, 01 Sep 2001 17:31:58 -0500
--Boundary_(ID_Yr8NzmNZIMaKfOzSFSgd/g)
Content-type: text/plain; charset=us-ascii
Content-transfer-encoding: 7BIT
I was treating StringVar() as if it were a global. over the couse of repeated attempts to get this working i stuck it several places in an effort to see if/how it worked. The problem as i understood it was a namespace issue preventing the thing from working. I have the search as a class as well. (weak OOP but i'm trying) so I'm sucessful on calling the class method from the root button now. I need to send or capture the output to the display. it's printing to the stdout window
I'm just one of those folks that has to USE it to understand it (I did BTW work your tutorial early on and it was quite helpful. Maybe a re-read is in order?)
*********** REPLY SEPARATOR ***********
On 9/1/01 at 5:55 PM alan.gauld@bt.com wrote:
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/tutname.htm I'll look at it, all help is apprecated
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...?
Teach Yourself Python 24 uses the above method, Programming Python the below. I tried several books to get an understanding. Depending on where I was looking it's inconsistant. I plan to clean it up when it works.
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
--Boundary_(ID_Yr8NzmNZIMaKfOzSFSgd/g)
Content-type: text/html; charset=us-ascii
Content-transfer-encoding: 7BIT
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=windows-1252">
<META content="MSHTML 5.50.4611.1300" name=GENERATOR></HEAD>
<BODY style="FONT-FAMILY: Arial" text=#000000 bgColor=#ffffff>
<DIV>I was treating StringVar() as if it were a global. over the couse of
repeated attempts to get this working i stuck it several places in an effort to
see if/how it worked. The problem as i understood it was a namespace issue
preventing the thing from working. I have the search as a class as well. (weak
OOP but i'm trying) so I'm sucessful on calling the class method from the
root button now. I need to send or capture the output to the display. it's
printing to the stdout window </DIV>
<DIV> </DIV>
<DIV>I'm just one of those folks that has to USE it to understand it (I did BTW
work your tutorial early on and it was quite helpful. Maybe a re-read is in
order?)</DIV>
<DIV> </DIV>
<DIV> </DIV>
<DIV><BR><FONT face=Arial size=2>*********** REPLY SEPARATOR
***********<BR><BR>On 9/1/01 at 5:55 PM alan.gauld@bt.com wrote:</FONT></DIV>
<BLOCKQUOTE
style="PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #000000 2px solid">
<BLOCKQUOTE
style="PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #0000ff 2px solid; MARGIN-RIGHT: 0px">
<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 face="Courier New"
color=#0000ff size=2> </FONT></SPAN><SPAN class=910503716-01092001><FONT
face="Courier New" color=#0000ff size=2>This is never used</FONT> <FONT
face="Courier New" color=#0000ff 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
face="Courier New" color=#0000ff 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 face="Courier New" color=#0000ff
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="PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #0000ff 2px solid; MARGIN-RIGHT: 0px">
<DIV>def search():<BR> sTerm=StringVar()<SPAN
class=910503716-01092001><FONT face="Courier New" color=#0000ff
size=2> </FONT></SPAN></DIV></BLOCKQUOTE>
<DIV><FONT face="Courier New" color=#0000ff size=2><SPAN
class=910503716-01092001>Not sure why you are using StrinGVar() here, you
don't </SPAN></FONT></DIV>
<DIV><FONT face="Courier New" color=#0000ff size=2><SPAN
class=910503716-01092001></SPAN></FONT><FONT face="Courier New" color=#0000ff
size=2><SPAN class=910503716-01092001>seem to use the 'magical' properties of
StringVar, </SPAN></FONT></DIV>
<DIV><FONT face="Courier New" color=#0000ff size=2><SPAN
class=910503716-01092001></SPAN></FONT><FONT face="Courier New" color=#0000ff
size=2><SPAN class=910503716-01092001>so a normal string would
suffice...</SPAN></FONT></DIV>
<DIV><FONT face="Courier New" color=#0000ff size=2><SPAN
class=910503716-01092001></SPAN></FONT><BR>
sTerm=box.entry.get() #produces attribute error as below<SPAN
class=910503716-01092001><FONT face="Courier New" color=#0000ff
size=2> </FONT></SPAN></DIV>
<DIV><SPAN class=910503716-01092001></SPAN> </DIV>
<DIV><FONT face="Courier New" color=#0000ff size=2><SPAN
class=910503716-01092001>especially since you now replace the StringVar with a
string</SPAN></FONT></DIV>
<DIV><FONT face="Courier New" color=#0000ff 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
face="Courier New" color=#0000ff size=2></FONT></SPAN></DIV>
<DIV><SPAN class=910503716-01092001><FONT face="Courier New" color=#0000ff
size=2>Similarly this won't work coz display is a name only known
</FONT></SPAN></DIV>
<DIV><SPAN class=910503716-01092001><FONT face="Courier New" color=#0000ff
size=2>to the doSearch function.</FONT> </SPAN></DIV>
<DIV><SPAN class=910503716-01092001></SPAN> </DIV>
<DIV><FONT face="Courier New" color=#0000ff 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 face="Courier New" color=#0000ff size=2><SPAN
class=910503716-01092001></SPAN></FONT> </DIV>
<DIV><FONT face="Courier New" color=#0000ff size=2><SPAN
class=910503716-01092001><A
href="http://www.crosswinds.net/~agauld/tutname.htm">http://www.crosswinds.net/~agauld/tutname.htm</A>
<FONT color=#ff0000>I'll look at it, all help is
apprecated</FONT></SPAN></FONT></DIV>
<DIV><FONT face="Courier New" color=#0000ff size=2><SPAN
class=910503716-01092001></SPAN></FONT> </DIV>
<BLOCKQUOTE
style="PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #0000ff 2px solid; MARGIN-RIGHT: 0px">
<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 face="Courier New" color=#0000ff size=2><SPAN
class=910503716-01092001>Why not just:</SPAN></FONT></DIV>
<DIV><FONT face="Courier New" color=#0000ff size=2><SPAN
class=910503716-01092001></SPAN></FONT> </DIV>
<DIV><FONT face="Courier New" color=#0000ff size=2><SPAN
class=910503716-01092001>Button(fr, text='Search',
command=doSearch).pack()</SPAN></FONT></DIV>
<DIV><FONT face="Courier New" color=#0000ff size=2><SPAN
class=910503716-01092001></SPAN></FONT> </DIV>
<DIV><FONT face="Courier New" color=#0000ff size=2><SPAN
class=910503716-01092001>As you do below...?</SPAN></FONT></DIV>
<DIV><FONT face="Courier New" color=#0000ff size=2><SPAN
class=910503716-01092001></SPAN></FONT> </DIV>
<DIV><FONT face="Courier New" color=#ff0000 size=2><SPAN
class=910503716-01092001>Teach Yourself Python 24 uses the above method,
Programming Python the below. I tried several books to get an understanding.
Depending on where I was looking it's inconsistant. I plan to clean it up when
it works.</SPAN></FONT></DIV>
<BLOCKQUOTE
style="PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #0000ff 2px solid; MARGIN-RIGHT: 0px">
<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 face="Courier New" color=#0000ff size=2><SPAN
class=910503716-01092001>So what the program does is create a window with 2
buttons and </SPAN></FONT></DIV>
<DIV><FONT face="Courier New" color=#0000ff size=2><SPAN
class=910503716-01092001>a text area. When you press the Sarch button it
creates a dialog</SPAN></FONT></DIV>
<DIV><FONT face="Courier New" color=#0000ff size=2><SPAN
class=910503716-01092001>with a label, entry, text area and
button.</SPAN></FONT></DIV>
<DIV><FONT face="Courier New" color=#0000ff size=2><SPAN
class=910503716-01092001></SPAN></FONT> </DIV>
<DIV><FONT face="Courier New" color=#0000ff size=2><SPAN
class=910503716-01092001>When you hit the dialog button it calls search which
has no </SPAN></FONT></DIV>
<DIV><FONT face="Courier New" color=#0000ff size=2><SPAN
class=910503716-01092001>knowledge </SPAN></FONT><FONT face="Courier New"
color=#0000ff size=2><SPAN class=910503716-01092001>of the dialog you created
only the original </SPAN></FONT></DIV>
<DIV><FONT face="Courier New" color=#0000ff size=2><SPAN
class=910503716-01092001>window(which is </SPAN></FONT><FONT
face="Courier New" color=#0000ff size=2><SPAN class=910503716-01092001>global
in scope. But Search tries to </SPAN></FONT></DIV>
<DIV><FONT face="Courier New" color=#0000ff size=2><SPAN
class=910503716-01092001>read the value in the </SPAN></FONT><FONT
face="Courier New" color=#0000ff size=2><SPAN class=910503716-01092001>dialog.
You either need to pass the </SPAN></FONT></DIV>
<DIV><FONT face="Courier New" color=#0000ff size=2><SPAN
class=910503716-01092001>entry and display widgets </SPAN></FONT><FONT
face="Courier New" color=#0000ff size=2><SPAN class=910503716-01092001>to the
search fnction or make </SPAN></FONT></DIV>
<DIV><FONT face="Courier New" color=#0000ff size=2><SPAN
class=910503716-01092001>them globally available.</SPAN></FONT></DIV>
<DIV><FONT face="Courier New" color=#0000ff size=2><SPAN
class=910503716-01092001></SPAN></FONT> </DIV>
<DIV><FONT face="Courier New" color=#0000ff size=2><SPAN
class=910503716-01092001>HTH,</SPAN></FONT></DIV>
<DIV><FONT face="Courier New" color=#0000ff size=2><SPAN
class=910503716-01092001></SPAN></FONT> </DIV>
<DIV><FONT face="Courier New" color=#0000ff size=2><SPAN
class=910503716-01092001>Alan G</SPAN></FONT></DIV><FONT size=2
Arial></BLOCKQUOTE></FONT></BODY></HTML>
--Boundary_(ID_Yr8NzmNZIMaKfOzSFSgd/g)--