2017-01-11 3 views
1

私はさまざまなログデータを含むTextCtrlを持っていますが、ユーザーが検索する文字列を検索できるEditTextフィールドもあります。検索ボタンをクリックすると、ログに記録されます。ブラウザで標準の検索/ハイライト/メモ帳などPython TextCtrl検索とハイライト機能

私はすでに動作しており、成功した利用者の言葉を強調したコードは、しかし私は実装したいと行方不明のビットがいくつかあります:

  • は、 同じ単語を検索し、次の単語をハイライト表示するなどの機能があります。 EDIT:これは以下のコードで[次を検索]ボタンを追加することで解決しました。このカウントは、次のハイライトをログの終わりまでではなく1ワードに制限します。
  • はそれを同じ単語、または新しい単語
  • スタートすること、新しい単語が検索され、現在のワードをアンハイライト位置0(データの先頭)新しい単語を EDITを検索する場合に: DEF

    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

findTxt内startPos値をリセットすることで解決:以下WX検索とハイライト機能の 新しい完全なコード。それはきれいではありませんが、機能します。

  • 同じ単語を検索する機能、および次の単語が例えば

"次を検索]をEDITを強調している:これは、以下の付いたボタン「次へ検索]に追加することで解決しましたコード。このカウントは、次のハイライトをデータの終わりまでではなく1ワードに制限します。

  • 新しい単語を検索する場合、現在のワードをアン強調それは同じ単語である、または新しい単語

EDIT:値を保持する2つの新しいグローバル変数を作成することによって解決さ新しい単語を検索する場合は、古い位置と単語の長さ、そして、新しい単語を見つける前に/黒、白のハイライトをrecolouringの

  • は0(データの先頭)の位置を開始

EDIT:DEF

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 
findTxt内部startPos値をリセットすることで解決