[wxPython-users] Re: text logging widget?

Josiah Carlson jcarlson at uci.edu
Fri Dec 15 12:35:04 PST 2006


Grant Edwards <grante at visi.com> wrote:
> 
> On 2006-12-14, Josiah Carlson <jcarlson at uci.edu> wrote:
> 
> > Digging into this further, the following seems to work for me in Python
> > 2.3 and wxPython 2.6.3 on Windows....
> >
> >
> >     self.SetInsertionPointEnd()
> >     self.WriteText(text)
> 
> Is that any different than self.AppendText(text)?

I remember using it because there was a slightly different behavior, but
I can't remember what that behavior was.  Try both of them.

> >     linecount = self.GetNumberOfLines()
> >     lp = lastpos = self.GetLastPosition()
> >     for i in xrange(min(2, linecount)):
> >         linecount -= 1
> >         lastpos -= self.GetLineLength(linecount)
> >     self.ShowPosition(lastpos + (lp != lastpos and
> >                      bool(self.GetLineLength(linecount))))
> >
> > In my case, my output tends to carry an extra newline, so what I really
> > want to see is somewhere around the second to last line.  The crap
> > inside the .ShowPosition call is just to make sure we are not trying to
> > view a newline.
> >
> > Note that if you get a bunch of text at once, it will probably jump to
> > view the last two lines.  Also, if you aren't getting scrollbars, you
> > may need to force a size event just after the text control is first
> > created.
> 
> Hmm. I'm a little baffled by the for loop.  I think I see what
> you're trying to do, but if all you want to do is not view a
> trailing newline, this seems to work fine:
> 
>  log.AppendText(text)
>  log.ShowPosition(log.GetLastPosition() - text.endswith('\n')*(log.GetLineLength(log.GetNumberOfLines()-2)+1))
> 
> Or, perhaps a bit easer to read:
> 
>  log.AppendText(text)
>  pos = log.GetLastPosition()
>  if text.endswith('\n'):
>      pos -= 1    # backup past trailing newline
>      pos -= log.GetLineLength(log.GetNumberOfLines()-2)  # backup to start of previous line
>  log.ShowPosition(pos)

I suppose with OS 9 and previous not really existing in the realm of
Python/wxPython, \r line endings shouldn't be anything to worry about. 
Yours should do more or less what I was expressing, though the code I
provided should select some position in the last or second to the last
line, not necessarily the first character (which may or may not be a
newline, I don't know the semantics of TextCtrl behavior).

 - Josiah





More information about the wxpython-users mailing list