wxpythonwxtextctrl

How to scroll to the bottom of a wxTextCtrl and redraw screen


I have a wxTextCtrl with many lines of text with scrollbars enabled. Inside an event, I'd like to scroll to the end of the control and redraw the control.

Here's what I have:

    def event_scroll_to_end(self, event):
        self.m_textCtrl1.SetScrollPos(
            wx.VERTICAL,
            self.m_textCtrl1.GetScrollRange(wx.VERTICAL))
        event.Skip()

This scrolls to the end and updates/redraws the scroll bar itself, but it doesn't update the textCtrl, which still shows scrolled to its current position.

How can I actually scroll the textCtrl as well so that the contents are scrolled to the end, as the scrollbar indicates?


Solution

  • I suspect that you need to set the insertion point, if you want to be positioned at the end of the text i.e.

    def event_scroll_to_end(self, event):
        self.m_textCtrl1.SetScrollPos(
            wx.VERTICAL,
            self.m_textCtrl1.GetScrollRange(wx.VERTICAL))
        self.m_textCtrl1.SetInsertionPoint(-1)
        event.Skip()
    

    Use SetInsertionPoint(0) to position yourself at the start of the text.