[Tutor] Text Boxes - deleting and inserting
Danny Yoo
dyoo at hkn.eecs.berkeley.edu
Sun Oct 24 01:29:26 CEST 2004
On Fri, 22 Oct 2004 seanf at email.arizona.edu wrote:
> 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.
Hi Sean,
Welcome aboard! Glad that you're enjoying Python.
> 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
Ah, this one isn't too bad. END is defined in the Tkinter module, so you
either need to fully qualify it:
self.delete(1.0, Tkinter.END)
or yank it into the namespace, during the import:
from Tkinter import Text, END
On a side note, there's a block of code here that can be improved:
> 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'
Repeated string concatentation can be expensive, depending on how large
self.myWidth and self.myHeight gets.
You may want to use a "buffer" approach to accumulate that string
variable instead. In Java, there's a java.lang.StringBuffer class that's
designed for doing this kind of accumulation. Python has a similar
feature that using a string's built-in join() method:
###
>>> buffer = []
>>> buffer.append("hello, this is")
>>> buffer.append("\n")
>>> buffer.append("a test")
>>> ''.join(buffer)
'hello, this is\na test'
###
I hope this helps!
More information about the Tutor
mailing list