[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&nbsp; 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&nbsp;sucessful on calling the class method from the 
root button now. I need to&nbsp;send or capture the output to the display. it's 
printing to the stdout window&nbsp;</DIV>
<DIV>&nbsp;</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>&nbsp;</DIV>
<DIV>&nbsp;</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>&nbsp;</DIV></BLOCKQUOTE>
  <DIV style="MARGIN-RIGHT: 0px">def doSearch(event):<BR>&nbsp;&nbsp;&nbsp; 
  sTerm=StringVar()<SPAN class=910503716-01092001><FONT face="Courier New" 
  color=#0000ff size=2>&nbsp;</FONT></SPAN><SPAN class=910503716-01092001><FONT 
  face="Courier New" color=#0000ff size=2>This is never used</FONT>&nbsp;<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>&nbsp;&nbsp;&nbsp; 
  box=Toplevel()<BR>&nbsp;&nbsp;&nbsp;<SPAN class=910503716-01092001>&nbsp;<FONT 
  face="Courier New" color=#0000ff size=2>&nbsp;</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&nbsp;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>&nbsp;</DIV>
  <DIV style="MARGIN-RIGHT: 0px">&nbsp;&nbsp;&nbsp; label=Label(box, text='Enter 
  term').pack()<BR>&nbsp;&nbsp;&nbsp; entry=Entry(box)<BR>&nbsp;&nbsp;&nbsp; 
  Button(box, text='Look For', command=search).pack()<SPAN 
  class=910503716-01092001><FONT face="Courier New" color=#0000ff 
  size=2>&nbsp;</FONT></SPAN></DIV>
  <DIV style="MARGIN-RIGHT: 0px">&nbsp;&nbsp;&nbsp; 
  entry.pack()<BR>&nbsp;&nbsp;&nbsp; entry.focus()<BR>&nbsp;&nbsp;&nbsp; 
  display=ScrolledText(box, width=40, height=20, bg='white').pack(expand=YES, 
  fill=BOTH)<BR>&nbsp;&nbsp;&nbsp; <BR>def quit():<BR>&nbsp;&nbsp;&nbsp; 
  root.quit()</DIV>
  <BLOCKQUOTE 
  style="PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #0000ff 2px solid; MARGIN-RIGHT: 0px">
    <DIV>def search():<BR>&nbsp;&nbsp;&nbsp; sTerm=StringVar()<SPAN 
    class=910503716-01092001><FONT face="Courier New" color=#0000ff 
    size=2>&nbsp;</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>&nbsp;&nbsp;&nbsp; 
  sTerm=box.entry.get()&nbsp; #produces attribute error as below<SPAN 
  class=910503716-01092001><FONT face="Courier New" color=#0000ff 
  size=2>&nbsp;</FONT></SPAN></DIV>
  <DIV><SPAN class=910503716-01092001></SPAN>&nbsp;</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>&nbsp;&nbsp;&nbsp; 
  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>&nbsp;</SPAN></DIV>
  <DIV><SPAN class=910503716-01092001></SPAN>&nbsp;</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>&nbsp;</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>&nbsp; 
  <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>&nbsp;</DIV>
  <BLOCKQUOTE 
  style="PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #0000ff 2px solid; MARGIN-RIGHT: 0px">
    <DIV><SPAN 
    class=910503716-01092001>&nbsp;</SPAN>root=Tk()<BR>fr=Frame(root)<BR>button=Button(fr)<BR>button['text']= 
    'Search'<BR>button.bind('&lt;Button-1&gt;', doSearch)<BR>button.pack()</DIV>
    <DIV>&nbsp;</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>&nbsp;</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>&nbsp;</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>&nbsp;</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>&nbsp;</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>&nbsp;</DIV>
    <DIV>root.mainloop()</DIV>
    <DIV>&nbsp;</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>&nbsp;</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>&nbsp;</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>&nbsp;</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)--