Scrolling a wxTextCtrl as text is added with AppendText()

I am adding text periodically to the bottom of a multiline wxTextCtrl, and need to scroll the window so that the new text appears at the bottom of the visible area. Obviously if there's not enough text to fill the window yet then it'll not be able to scroll.

Or to put it another way, I want it to act as a normal text editor or word processor does, except I'll be adding the text with AppendText() rather than typing it directly.

self.output.ShowPosition(self.output.GetLastPosition()) seems to place the current line at the top of the window which is not what I want. Adding self.output.PageUp() after it seems to be close to what I want, but sometimes it simply doesn't scroll when new data is added and I don't know why. The scrollbar proportions change accordingly, but the viewport doesn't always seem to move.

Could anybody tell me how to get the effect I need?

···

--
Ben Sizer
http://pages.eidosnet.co.uk/kylotan

Never mind, the inconsistency in the scrolling was a bug elsewhere, and for what it's worth I got something satisfactory with this code:

  self.output.Freeze() # stop updates
  self.output.AppendText(text)
  self.output.ShowPosition(self.output.GetLastPosition())
  self.output.PageUp()
  self.output.ScrollLines(1)
  self.output.Thaw()

···

--
Ben Sizer
http://pages.eidosnet.co.uk/kylotan

Ben Sizer wrote:

self.output.Freeze() # stop updates
self.output.AppendText(text)
self.output.ShowPosition(self.output.GetLastPosition())
self.output.PageUp()
self.output.ScrollLines(1)
self.output.Thaw()

Ok, Freeze() doesn't seem to be fully supported on all versions of Windows, never mind what I might find on other platforms. So in testing I get the output window moving up and down when I add data. Is there some other way of scrolling to the bottom?

···

--
Ben Sizer
http://pages.eidosnet.co.uk/kylotan

Ben Sizer writes:

Ok, Freeze() doesn't seem to be fully supported on all
versions of Windows, never mind what I might find on other
platforms. So in testing I get the output window moving up
and down when I add data. Is there some other way of
scrolling to the bottom?

You know, I don't know how this would be done in wx, but if you
could stuff a keypress of Ctrl+End in the keyboard buffer,
while the TextCtrl you want to scroll has the focus, that would
be a poor-man's solution to your problem... that is of course
if Ctrl+End works similarly on all platforms (pretty sure it
does).

···

--
Paul

Paul McNett wrote:

You know, I don't know how this would be done in wx, but if you could stuff a keypress of Ctrl+End in the keyboard buffer, while the TextCtrl you want to scroll has the focus, that would be a poor-man's solution to your problem... that is of course if Ctrl+End works similarly on all platforms (pretty sure it does).

Hmm. I added this code:

  ctrlEnd = wxKeyEvent(wxEVT_CHAR)
  ctrlEnd.m_controlDown = True
  ctrlEnd.m_keyCode = WXK_END
  self.output.EmulateKeyPress(ctrlEnd)

While it seems to have the correct visual effect, it also happens to start sending almost random characters to whatever control is focused - even one in a totally different application! When the above code triggered I found I was getting various characters appear in the currently selected text box... when I task-switched back to IDLE I found that IDLE was receiving various key events! And on other occasions I found it opening up my Windows Run dialog, among other things.

So I'm guessing there's some sort of bug here, either in my code or in wxWindows. Looks like the key event is not being sent to the right place, and the actual event sent seems somewhat arbitrary. I'm running on Win98 SE.

···

--
Ben Sizer
http://pages.eidosnet.co.uk/kylotan

Ben Sizer writes:

when I task-switched back to IDLE

You are using IDLE with wxPython? Is that possible/recommended?

···

--
Paul

Paul McNett wrote:

You are using IDLE with wxPython? Is that possible/recommended?

What difference should it make? Everything seems to work fine.

···

--
Ben Sizer
http://pages.eidosnet.co.uk/kylotan

See wxPython WIKI.

Most Debuggers (like IDLE or Python Win) use a different GUI toolkit than wxPython does, and some debug the wxPython app within their own process. This creates lots of conflicts between the wxPython event loop and the event loop of the debuggers' GUI tookit.

So, you need a debugger that debugs your program out-of-process. At present, HAP, Wing IDE and Komodo do so.

HAP is open-source, so you might be interested in checking that one out; see http://hapdebugger.sourceforge.net/

Or use an IDE based on wxPython like Boa Constructor.

See you
Werner

Ben Sizer wrote:

···

Paul McNett wrote:

You are using IDLE with wxPython? Is that possible/recommended?

What difference should it make? Everything seems to work fine.

Werner F. Bruhin wrote:

So, you need a debugger that debugs your program out-of-process. At present, HAP, Wing IDE and Komodo do so.

HAP won't run on my system since it requires Win2000.
Wing IDE costs $179.00, so that's not happening.
Komodo costs $295; see above.

Or use an IDE based on wxPython like Boa Constructor.

Boa Constructor chews up my system resources like you wouldn't believe, so I'm avoiding it these days.

However, since I'm not doing any debugging as such, I don't think this is really a problem for me. I can confirm that the problem with the keypresses occurs when I run the Python script directly, not just when executing it from IDLE.

···

--
Ben Sizer
http://pages.eidosnet.co.uk/kylotan

Ben Sizer wrote:

Ben Sizer wrote:

self.output.Freeze() # stop updates
self.output.AppendText(text)
self.output.ShowPosition(self.output.GetLastPosition())
self.output.PageUp()
self.output.ScrollLines(1)
self.output.Thaw()

Ok, Freeze() doesn't seem to be fully supported on all versions of Windows, never mind what I might find on other platforms. So in testing I get the output window moving up and down when I add data. Is there some other way of scrolling to the bottom?

AppendText by itself is supposed to do what you are trying to do. What style flags are you using on the text ctrl? I think that one of the wxTE_RICH* styles could cause the autoscroll to not work though so maybe you should play with that a bit.

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!

Robin Dunn wrote:

Ben Sizer wrote:

Ok, Freeze() doesn't seem to be fully supported on all versions of Windows, never mind what I might find on other platforms. So in testing I get the output window moving up and down when I add data. Is there some other way of scrolling to the bottom?

AppendText by itself is supposed to do what you are trying to do. What style flags are you using on the text ctrl? I think that one of the wxTE_RICH* styles could cause the autoscroll to not work though so maybe you should play with that a bit.

I'm using wxTE_MULTILINE|wxTE_RICH|wxTE_READONLY. I need one of the richedit controls as being able to colour my text is vital.

Checking the list archives I now see that a couple of months ago, Susanne Lefvert had the same scrolling issues that I do. I suppose that if I could find an insert position on the first visible line, I could use ShowPosition() with that. But I can't work out how to get a position on the appropriate line.

···

--
Ben Sizer
http://pages.eidosnet.co.uk/kylotan

IIRC, AppendText doesn't work correctly with wxTE_READONLY. You have to call
SetEditable(True), then call AppendText, then SetEditable(False). I wouldn't
be surprised if behavior varies depending on which wxTE_RICH style you use
and of course each platform will be different too.

There is another scroll bug where you need to call ScrollLines(-1) to fix
the scrollbars.

http://sourceforge.net/tracker/?func=detail&aid=665381&group_id=9863&atid=10
9863

ka

···

-----Original Message-----
From: Ben Sizer [mailto:brsizer@kylotan.eidosnet.co.uk]
Sent: Friday, December 05, 2003 1:09 PM
To: wxPython-users@lists.wxwindows.org
Subject: Re: [wxPython-users] Scrolling a wxTextCtrl as text is added
with AppendText()

Robin Dunn wrote:
> Ben Sizer wrote:
>
>> Ok, Freeze() doesn't seem to be fully supported on all versions of
>> Windows, never mind what I might find on other platforms. So in
>> testing I get the output window moving up and down when I add data. Is
>> there some other way of scrolling to the bottom?
>
> AppendText by itself is supposed to do what you are trying to do. What
> style flags are you using on the text ctrl? I think that one of the
> wxTE_RICH* styles could cause the autoscroll to not work though
so maybe
> you should play with that a bit.

I'm using wxTE_MULTILINE|wxTE_RICH|wxTE_READONLY. I need one of the
richedit controls as being able to colour my text is vital.

Checking the list archives I now see that a couple of months ago,
Susanne Lefvert had the same scrolling issues that I do. I suppose that
if I could find an insert position on the first visible line, I could
use ShowPosition() with that. But I can't work out how to get a position
on the appropriate line.

--
Ben Sizer
http://pages.eidosnet.co.uk/kylotan

---------------------------------------------------------------------
To unsubscribe, e-mail: wxPython-users-unsubscribe@lists.wxwindows.org
For additional commands, e-mail: wxPython-users-help@lists.wxwindows.org

Kevin Altis wrote:

IIRC, AppendText doesn't work correctly with wxTE_READONLY. You have to call
SetEditable(True), then call AppendText, then SetEditable(False). I wouldn't
be surprised if behavior varies depending on which wxTE_RICH style you use
and of course each platform will be different too.

I added the SetEditable lines and removed my other workarounds to test it on my Win98 SE system.

Using wxTE_RICH and just the SetEditable stuff, no scrolling occured at all.
Using wxTE_RICH2 and just the SetEditable stuff, scrolling took place at page limits only, when it would scroll an entire page leaving the bottom of the window empty.

There is another scroll bug where you need to call ScrollLines(-1) to fix
the scrollbars.

http://sourceforge.net/tracker/?func=detail&aid=665381&group_id=9863&atid=10
9863

Ok... so I added this and the results were:

wxTE_RICH + SetEditable + ScrollLines(-1) = still no scrolling at all.
wxTE_RICH2 + SetEditable + ScrollLines(-1) = it worked properly, following the text at the bottom of the screen as intended.

This last solution also works on Win XP Home, which is the OS that didn't respect Freeze() in conjunction with AppendText / ShowPosition / PageUp / ScrollLines using wxTE_RICH (my original solution that worked on 98 SE).

Thanks for the advice.

···

--
Ben Sizer
http://pages.eidosnet.co.uk/kylotan