WxPython将新文本行添加到WxTextCtrl导致失去治疗位置

时间:2018-01-21 15:07:40

标签: wxpython

当不断打印到WxTextCtrl时,如何禁用新打印,导致跳转到TextCtrl的末尾?

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

我已经为DUT构建了一个LOG TextCtrl。

当我向上滚动时,它会在每条新消息上“跳回”TextCtrl的底部。当我滚动时如何禁用它?

1 个答案:

答案 0 :(得分:1)

使用SetInsertionPointGetSelection 这似乎做你想要的。
请注意,检查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()