0

When printing constantly to WxTextCtrl, how do i disable a new print causing the jump to the end of the TextCtrl?

self.command_line = wx.TextCtrl(pnl, -1, style = wx.TE_MULTILINE | wx.TE_PROCESS_ENTER)

I have build a LOG TextCtrl to a DUT.

When i scroll up, it keeps "jumping" back to the bottom of TextCtrl on every new message. how can i disable that when i scroll?

schanti schul
  • 681
  • 3
  • 14

1 Answers1

1

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()
Rolf of Saxony
  • 21,661
  • 5
  • 39
  • 60
  • Thanks , but now it jumps to the top on every new message. – schanti schul Jan 22 '18 at 13:40
  • My goal is to be able to read some text from the log while it continues printing more lines.I basically scroll and what it to stay focused on that part. – schanti schul Jan 22 '18 at 13:41
  • Then set the insertion point to the Scroll position before you appended to it. This should be available via `GetScrollPosition()`, although it fails miserably using wxpython4, as it falls over stating that the "window is not scrollable" in spite of the fact that it clearly has a vertical scrollbar. You might get more mileage from a scrolled panel. – Rolf of Saxony Jan 23 '18 at 19:58
  • thanks, ended up tweaking this code : [link](https://stackoverflow.com/questions/153989/how-do-i-get-the-scroll-position-range-from-a-wx-textctrl-control-in-wxpython) – schanti schul Jan 25 '18 at 23:08