Python TextCtrl搜索和高亮显示功能
我有一个包含各种日志数据的TextCtrl,还有一个EditText字段,用户可以通过该字段搜索要查找的字符串,然后单击find按钮查找并高亮显示在日志中找到的单词。您的标准查找/突出显示在浏览器/记事本等 我的代码已经成功地突出显示了用户的单词,但是我想实现的代码中缺少了几个位:Python TextCtrl搜索和高亮显示功能,python,wxpython,wxtextctrl,Python,Wxpython,Wxtextctrl,我有一个包含各种日志数据的TextCtrl,还有一个EditText字段,用户可以通过该字段搜索要查找的字符串,然后单击find按钮查找并高亮显示在日志中找到的单词。您的标准查找/突出显示在浏览器/记事本等 我的代码已经成功地突出显示了用户的单词,但是我想实现的代码中缺少了几个位: 搜索同一个单词并突出显示下一个单词的功能,例如“查找下一个”编辑:通过添加带有以下代码的“查找下一个”按钮解决了此问题。计数将下一个突出显示限制为1个字,而不是将所有突出显示限制为日志末尾 搜索新词时取消高亮显示当
- 搜索同一个单词并突出显示下一个单词的功能,例如“查找下一个”编辑:通过添加带有以下代码的“查找下一个”按钮解决了此问题。计数将下一个突出显示限制为1个字,而不是将所有突出显示限制为日志末尾
- 搜索新词时取消高亮显示当前词,无论是同一个词还是新词
- 如果搜索新词,则从0(数据顶部)开始编辑:通过重置
def中的startPos值来解决findTxt
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)
- 搜索同一单词并突出显示下一个单词的能力,例如“查找下一个”
- 搜索新词时取消高亮显示当前词,无论是同一个词还是新词
- 如果搜索新词,则从0(数据顶部)开始
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
符合所有标准:
wx搜索和突出显示功能的新完整代码如下。虽然不漂亮,但很管用
- 搜索同一单词并突出显示下一个单词的能力,例如“查找下一个”
- 搜索新词时取消高亮显示当前词,无论是同一个词还是新词
- 如果搜索新词,则从0(数据顶部)开始
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
在这里结束的任何人都可能希望看到前面解释的
wx.findreplace对话框
。在这里结束的任何人也可能希望看到前面解释的wx.findreplace对话框
。