【问题标题】:WxPython Add new text line to WxTextCtrl causes to lose curernt locationWxPython 向 WxTextCtrl 添加新文本行导致丢失当前位置
【发布时间】:2018-01-21 15:07:40
【问题描述】:

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

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

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

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

【问题讨论】:

    标签: wxpython


    【解决方案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()
    

    【讨论】:

    • 谢谢,但现在它会跳到每条新消息的顶部。
    • 我的目标是能够在日志继续打印更多行的同时从日志中读取一些文本。我基本上滚动并关注该部分的内容。
    • 然后在添加之前将插入点设置为滚动位置。这应该可以通过GetScrollPosition() 获得,尽管它在使用 wxpython4 时失败了,因为尽管它显然有一个垂直滚动条,但它却说“窗口不可滚动”。您可能会从滚动面板获得更多里程。
    • 谢谢,最终调整了这段代码:link
    猜你喜欢
    • 2020-03-06
    • 2021-03-07
    • 1970-01-01
    • 2013-04-05
    • 2015-03-05
    • 2020-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多