[Tutor] Text Boxes - deleting and inserting

seanf at email.arizona.edu seanf at email.arizona.edu
Sat Oct 23 08:00:33 CEST 2004


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.

Sincerely,
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."




More information about the Tutor mailing list