[Tutor] Text Boxes - deleting and inserting

Kent Johnson kent_johnson at skillsoft.com
Sat Oct 23 12:44:05 CEST 2004


END is defined in the Tkinter module. You need to import it to use it.

My guess is that the version that worked had
from Tkinter import *

What that does is import all the names defined by the Tkinter module into 
the current namespace. So things like Tkinter.Text and Tkinter.END are 
accessible through the bare names Text and END.

Your current program has
from Tkinter import Text

This just makes one name from Tkinter available in the current scope - Text.
You should change this to say
from Tkinter import Text, END

or just the blanket import
from Tkinter import *

You might want to review the Tutorial section on imports: 

Looking at the rest of your code, you are overusing self (abusing your 
self? ;). Normally you only use self for variables that need to be shared 
between two methods or that need to persist between method calls. Temporary 
variables don't have to be stored in the instance. For example this code
>         self.s = ""
>         for self.i in range(self.myWidth):
>             for self.j in range(self.myHeight):
>                 self.s = self.s + model.getChar(self.i, self.j)
>             self.s = self.s + '\n'
>         print self.s
>         self.insert(END, self.s)

would be better written without all those self's. The only ones needed are 
to access myWidth and myHeight (which may not need to be member variables 
either) and at the end to call self.insert():
         s = ""
         for i in range(self.myWidth):
             for j in range(self.myHeight):
                 s = s + model.getChar(i, j)
             s = s + '\n'

         print s
         self.insert(END, s)

BTW Python has what it takes to keep the flame burning for many years, but 
it'll spoil you. Java looks like an ugly hack in comparison and I can't 
bear to even look at C++.

If you want help or suggestions about using Python in CS, you might want to 
venture over to edu-sig, lots of folks over there think Python is far 
better than Java as a first teaching language.


At 11:00 PM 10/22/2004 -0700, seanf at email.arizona.edu wrote:
>Hey guys,
>First of all a quick intro: My name is Sean Fioritto. I'm a CS major at the
>University of Arizona. My dad has been trying to get me to learn Python since
>my freshman year...about 3 years ago. I was finally able to put all the books
>he gave me to use because we're using Python in my AI class - and I have 
>to say
>that if it were possible to have a crush on a programming language, I would be
>drooling over Python at this point. I've only been at it for about a week 
>and I
>already feel like buying Python t-shirts and telling all my CS professors to
>stop teaching Java and teach Python!
>So here is my problem. I'm creating a program that uses the model, view,
>controller pattern and the class that isn't working is going to be an
>obvserver. The text box will populate itself with a String that represents the
>model every time the model tells the text box to update. I decided to use
>Python's nifty multiple inheritance feature: so cView is both a Text 
>object and
>an Observer object. The problem is with the Text class, specifically in Update
>when it calls self.delete(1.0, END). This, according to google, should delete
>the entire box.
>Here's the error (m is a model object):
> >>> v = CritterView.cView()
> >>> v.update(m)
>Traceback (most recent call last):
>   File "<pyshell#26>", line 1, in -toplevel-
>     v.update(m)
>   File "C:\home\projects\CritterView.py", line 18, in update
>     self.delete(1.0, END)
>NameError: global name 'END' is not defined
>I've tried self.END to no avail. The most frustrating thing is that it worked
>before, then I changed something and broke it. When I create a regular Text
>object, t = Text(), and call t.delete(1.0, END), it doesn't puke all over me
>and works perfectly. Apparently Python just can't digest my code.
>I'm up for any suggestions. Also, if I'm doing anything goofy and 'unpythonic'
>let me know.
>Sean Fioritto
>Here is the code:
>from pObservable import Observer
>from Tkinter import Text
>class cView(Observer, Text):
>     def __init__(self, parent=None):
>         Observer.__init__(self)
>         Text.__init__(self, parent)
>         self.config(state='disabled')
>         self.pack()
>     def update(self, model, event = None, message=None):
>         self.myHeight = model.height
>         self.myWidth = model.width
>         self.config(state='normal')
>         self.delete(1.0, END)
>         self.s = ""
>         for self.i in range(self.myWidth):
>             for self.j in range(self.myHeight):
>                 self.s = self.s + model.getChar(self.i, self.j)
>             self.s = self.s + '\n'
>         print self.s
>         self.insert(END, self.s)
>         self.config(state='disabled')
>Albert Einstein, when asked to describe radio, replied:
>"You see, wire telegraph is a kind of a very, very long cat. You pull his tail
>in New York and his head is meowing in Los Angeles. Do you understand this?
>And radio operates exactly the same way: you send signals here, they receive
>them there. The only difference is that there is no cat."
>Tutor maillist  -  Tutor at python.org

More information about the Tutor mailing list