Search code examples
wxpythonscrollwxwidgetstextctrlwx.textctrl

How do I get the scroll position / range from a wx.TextCtrl control in wxPython


I have a little logging app (written in wxPython) that receives data from a bit of kit we're developing, and I want to display the text in a scrolling window. As it stands I'm using a wx.TextCtrl for the text display, but I'm having some issues with the scrolling behaviour.

Basically, I'd like it so that if the scrollbar is at the bottom of the window (i.e. the end of the incoming data), adding more data should scroll the view onwards. If, however the view has been scrolled up a little (i.e. the user is looking at something interesting like an error message), the app should just add the text on the end without scrolling any more.

I've got two problems at the moment:

  1. I can't work out how to retrieve the current scroll position (calls to GetScrollPos() don't seem to work - they just return 0).
  2. I can't work out how to retrieve the current range of the scroll bar (calls to GetScrollRange() just return 1).

I've googled a bit and there seem to be a few hints that suggest GetScrollPos and GetScrollRange won't work for a wx.TextCtrl? Has anyone else had any experience in this area? Is there a nice easy way to solve the problem or am I going to have to roll my own wx.TextCtrl?


Solution

  • I just tested a simple example (checking GetScrollPos(0) and GetScrollRange(0) in EVT_TEXT event handler for wx.TextCtrl) and it works fine for me - they return index of currently shown line and total number of lines, respectively.

    Maybe the problem is your wxPython version? I used:

    >>> import wx
    >>> wx.version()
    '2.8.9.1 (msw-unicode)'