1

我有一个包含各种日志数据的 TextCtrl,我还有一个 EditText 字段,用户可以在其中搜索要查找的字符串,然后单击 Find 按钮查找并突出显示在日志中找到的单词。您在浏览器/记事本等中的标准查找/突出显示。

我拥有的代码已经工作并成功突出了用户的单词,但是我想实现一些缺少的部分:

  • 能够搜索同一个词,并突出显示下一个词,例如“查找下一个” 编辑:这是通过使用以下代码添加“查找下一个”按钮来解决的。计数将下一个突出显示限制为 1 个单词,而不是全部到日志末尾。
  • 搜索新词时取消突出显示当前词,无论是同一个词,还是一个新词
  • 如果搜索新单词,则从 0(数据顶部)开始位置 编辑:通过重置findTxtdef内的 startPos 值来解决

    def findTxt(self,e):
    global wordPos
    newstring = self.logTxt.GetValue()
    for i in range(self.progressBox.GetNumberOfLines()):
        line = self.progressBox.GetLineText(i)
        if newstring in line:
            startPos = self.progressBox.GetValue().find(newstring)
            endPos = startPos + len(newstring) 
            wordPos = endPos
            self.progressBox.Bind(wx.EVT_SET_FOCUS, self.highlightText(startPos, endPos))
            startPos = 0
            self.findNextBtn.Enable() 
    
    def findNext(self,e):
    global wordPos
    newstring = self.logTxt.GetValue()
    count = 0
    for i in range(self.progressBox.GetNumberOfLines()):
        if count == 0: 
            line = self.progressBox.GetValue()
            if newstring in line:
                startPos = self.progressBox.GetValue().find(newstring, wordPos)
                endPos = startPos + len(newstring) 
                wordPos = endPos
                self.progressBox.Bind(wx.EVT_SET_FOCUS, self.highlightText(startPos, endPos))
                count = 1
    
    
    def highlightText(self, pos, size):
    self.progressBox.SetStyle(pos, size, wx.TextAttr("black", "turquoise"))
    self.progressBox.SetInsertionPoint(pos)
    
4

1 回答 1

0

满足所有条件: wx 搜索和突出显示功能的新完整代码如下。它不漂亮,但它有效。

  • 能够搜索同一个词,并突出显示下一个词,例如“查找下一个”

编辑:这是通过使用以下代码添加“查找下一个”按钮来解决的。计数将下一个突出显示限制为 1 个单词,而不是全部到数据末尾。

  • 搜索新词时取消突出显示当前词,无论是同一个词,还是一个新词

编辑:通过创建 2 个新的全局变量来解决,这些变量保存旧位置的值和单词的长度,然后在找到新单词之前将突出显示重新着色为黑色/白色

  • 如果搜索新单词,则从 0(数据顶部)开始位置

编辑:通过重置 findTxt def 中的 startPos 值解决

global wordPos
wordPos,oldPos = '',''

#wx widgets
self.progressBox = wx.TextCtrl(panelLog, style=wx.TE_MULTILINE|wx.TE_READONLY|wx.TE_RICH2)
    hBox2.Add(self.progressBox, 5, flag=wx.EXPAND)
    vBox.Add(hBox2, 2, flag=wx.EXPAND|wx.LEFT|wx.RIGHT|wx.BOTTOM, border=10)

    self.logTxt = wx.TextCtrl(panelLog, style=wx.TE_RICH)
    hBox3.Add(self.logTxt, 1, flag=wx.LEFT, border=5)

    self.findBtn = wx.Button(panelLog, -1, "Find")
    self.Bind(wx.EVT_BUTTON, self.findTxt, self.findBtn)
    hBox3.Add(self.findBtn, flag=wx.EXPAND|wx.LEFT|wx.RIGHT|wx.BOTTOM, border=3)

    self.findNextBtn = wx.Button(panelLog, -1, "Find Next")
    self.findNextBtn.Disable()
    self.Bind(wx.EVT_BUTTON, self.findNext, self.findNextBtn)
    hBox3.Add(self.findNextBtn, flag=wx.EXPAND|wx.LEFT|wx.RIGHT|wx.BOTTOM, border=3)
    vBox.Add(hBox3, 0, flag=wx.EXPAND|wx.LEFT|wx.RIGHT|wx.BOTTOM, border=10)

#method code
def findTxt(self,e):
    global wordPos, oldPos
    newstring = self.logTxt.GetValue()
    if wordPos:
        self.progressBox.SetStyle(oldPos, wordPos, wx.TextAttr("black", "white"))
    for i in range(self.progressBox.GetNumberOfLines()):
        line = self.progressBox.GetLineText(i)
        if newstring in line:
            startPos = self.progressBox.GetValue().find(newstring)
            endPos = startPos + len(newstring) 
            wordPos = endPos
            oldPos = startPos

            self.progressBox.Bind(wx.EVT_SET_FOCUS, self.highlightText(startPos, endPos))
            startPos = 0
            self.findNextBtn.Enable()


def findNext(self,e):
    global wordPos, oldPos
    newstring = self.logTxt.GetValue()
    self.progressBox.SetStyle(oldPos, wordPos, wx.TextAttr("black", "white"))
    count = 0
    for i in range(self.progressBox.GetNumberOfLines()):
        if count == 0: 
            line = self.progressBox.GetValue()
            if newstring in line:
                startPos = self.progressBox.GetValue().find(newstring, wordPos)
                endPos = startPos + len(newstring) 
                wordPos = endPos
                oldPos = startPos
                self.progressBox.Bind(wx.EVT_SET_FOCUS, self.highlightText(startPos, endPos))
                count = 1
于 2017-01-13T08:56:19.537 回答