Make use of the SetInsertionPoint
and GetSelection
.
This seems to do what you want.
Note that checking the end == pos
and resetting the insertion point, allows you to click at the bottom of the text, which will then continue with the normal scrolling, when text is added later.
import wx
class MyApp(wx.Frame):
def __init__(self, parent, title):
wx.Frame.__init__(self,parent, style = wx.DEFAULT_FRAME_STYLE,title=title, size=(500, 515))
sizer=wx.BoxSizer(wx.HORIZONTAL)
button = wx.Button(self, id = 1,label = "Add Text")
self.Bind(wx.EVT_BUTTON, self.OnButton, button)
self.note = "This is a fairly long text string that I would like to wrap. This is a fairly long text string that I would like to wrap. This is a fairly long text string that I would like to wrap. This is a fairly long text string that I would like to wrap. This is a fairly long text string that I would like to wrap. This is a fairly long text string that I would like to wrap. This is a fairly long text string that I would like to wrap. This is a fairly long text string that I would like to wrap. This is a fairly long text string that I would like to wrap."
self.noteCtrl = wx.TextCtrl(self,wx.ID_ANY,value=self.note,style=wx.TE_MULTILINE|wx.VSCROLL,size=(50, 400))
self.Bind(wx.EVT_TEXT, self.OnText, self.noteCtrl)
sizer.Add(self.noteCtrl,1)
sizer.Add(button,0)
self.SetSizer(sizer)
self.noteCtrl.SetInsertionPoint(0)
self.Show()
def OnButton(self,evt):
end = self.noteCtrl.GetLastPosition()
pos = self.noteCtrl.GetInsertionPoint()
if end == pos:
self.noteCtrl.SetSelection(0,0)
selected1, selected2 = self.noteCtrl.GetSelection()
self.noteCtrl.AppendText("\nxxxxxxxxxxxxxxxxxxxxxxxxxx\n")
self.noteCtrl.AppendText("xxxxxxxxxxxxxxxxxxxxxxxxxx\n")
self.noteCtrl.AppendText("xxxxxxxxxxxxxxxxxxxxxxxxxx\n")
self.noteCtrl.AppendText("xxxxxxxxxxxxxxxxxxxxxxxxxX\n")
if selected1 != 0:
self.noteCtrl.SetInsertionPoint(selected1)
def OnText(self,evt):
selected1, selected2 = self.noteCtrl.GetSelection()
print (selected1, selected2)
if __name__ == "__main__":
app = wx.App()
MainFrame = MyApp(None, title = "My Application")
app.MainLoop()