我有一个包含各种日志数据的 TextCtrl,我还有一个 EditText 字段,用户可以在其中搜索要查找的字符串,然后单击 Find 按钮查找并突出显示在日志中找到的单词。您在浏览器/记事本等中的标准查找/突出显示。
我拥有的代码已经工作并成功突出了用户的单词,但是我想实现一些缺少的部分:
能够搜索同一个词,并突出显示下一个词,例如“查找下一个”编辑:这是通过使用以下代码添加“查找下一个”按钮来解决的。计数将下一个突出显示限制为 1 个单词,而不是全部到日志末尾。- 搜索新词时取消突出显示当前词,无论是同一个词,还是一个新词
如果搜索新单词,则从 0(数据顶部)开始位置编辑:通过重置findTxt
def内的 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)