使用SetInsertionPoint 和GetSelection。
这似乎可以满足您的需求。
请注意,检查end == pos 并重置插入点,允许您单击文本底部,然后在稍后添加文本时继续正常滚动。
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()