【发布时间】:2017-05-26 08:31:19
【问题描述】:
我有一个包含各种日志数据的 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)
【问题讨论】:
标签: python wxpython wxtextctrl