0

我有一个脚本,它返回 gtk textview 对象中字符串的开始/结束位置。我觉得我一遍又一遍地做同样的事情。第一个搜索函数返回每个匹配位置的开始、结束。第二个我希望搜索并突出显示数组给出的文本视图中的每个对象。searchn() 我注意到我可以更改数组选择并选择第二个到最后一个或我选择的任何内容。

有没有一种方法可以每次单击数组中的每个对象一次选择一个,这样它将按顺序突出显示数组中的一个对象。

所以如果我有四个字

工作工作工作工作

然后我可以突出显示第一个,然后我再次单击它会重新运行该功能并突出显示第二个然后第三个,当它全部完成后,我可以让它回到开头。我在想有点像。for range in len(z): 这样做,但无论我在 for 循环中做什么,它都会选择最后一步的最后一步。我希望这是有道理的。

def search(x):
search_required = False
sbuffer.remove_tag_by_name('found', sbuffer.get_start_iter(), sbuffer.get_end_iter())
    find_text = findentry.get_text()
    if find_text:
  text = sbuffer.get_text(sbuffer.get_start_iter(),sbuffer.get_end_iter()).decode('utf8')
      search_matches = [(match.start(), match.end()) for match in re.finditer(re.escape(find_text), text, re.IGNORECASE)]
      if len(search_matches) < 1000:
    for start,end in search_matches:
      iter1 = sbuffer.get_iter_at_offset(start)
      iter2 = sbuffer.get_iter_at_offset(end)
      sbuffer.select_range(iter1, iter2)
      sbuffer.apply_tag_by_name('found', iter1, iter2)
    textview.scroll_to_mark(sbuffer.get_mark('insert'), 0.25)
    print search_matches
    return search_matches
else:
  search_matches = [ ]    



  def searchn(x):
search(x)
z=search(x)
#def SN(z)
print len(z)
z=z[:1]# need to have this do to 2nd,3rd,4th,5th ect till there are no more matches.
for a,l in z:
  print a,"start of this match"
  print l, "end of this match"
  iter1 = sbuffer.get_iter_at_offset(a)
  iter2 = sbuffer.get_iter_at_offset(l)
  sbuffer.select_range(iter1,iter2)
  print z
return z  

先感谢您!

4

2 回答 2

1

你为什么不使用正则表达式来查找你正在寻找的单词的实例,然后一旦你有了所有的索引就进行循环。

例子:

import re
sentence = "You are my favorite. Are you my friend? Are you an elephant?"
items = []
for item in re.finditer("[Aa]re", sentence, ):
    items.append(item.start())

这应该为您提供单词所在的位置。一旦你有了这些,你应该能够以任何你喜欢的方式快速轻松地穿过这些位置。

于 2013-04-07T22:20:44.770 回答
0

you know the idea did come to me in a dream a couple of nights ago. I just kept messing it up...:P here is is working. and Thank you JazzyWhite! Python search function forward and backward. I hope it helps anyone that would want to make something like this. For anyone else looking Base is my main class and sbuffer is textview.get_buffer() and yes I am using gtk.

  searchmatchp=None
  searchmatchf=None
  def search(x):
sbuffer.remove_tag_by_name('found', sbuffer.get_start_iter(), sbuffer.get_end_iter())
    find_text = findentry.get_text()
    if find_text:
  text = sbuffer.get_text(sbuffer.get_start_iter(),sbuffer.get_end_iter()).decode('utf8')
      search_matches = [(match.start(), match.end()) for match in re.finditer(re.escape(find_text), text, re.IGNORECASE)]
      if len(search_matches) < 1000:
    for start,end in search_matches:
      iter1 = sbuffer.get_iter_at_offset(start)
      iter2 = sbuffer.get_iter_at_offset(end)
      sbuffer.select_range(iter1, iter2)
      sbuffer.apply_tag_by_name('found', iter1, iter2)
    textview.scroll_to_mark(sbuffer.get_mark('insert'), 0.25)
    Base.searchmatchp=search_matches
    Base.searchmatchf=search_matches
    #return search_matches
else:
  search_matches = [ ]    



  def searchn(self):
print Base.searchmatchf
for a,l in Base.searchmatchf[:1]:
  iter1 = sbuffer.get_iter_at_offset(a)
  iter2 = sbuffer.get_iter_at_offset(l)
  sbuffer.select_range(iter1,iter2)
  textview.scroll_to_mark(sbuffer.get_mark('insert'), 0.25)
del Base.searchmatchf[:1]  
if Base.searchmatchf ==[]:
  search(self)




  def searchp(self):
print Base.searchmatchp
del Base.searchmatchp[-1:] 
print Base.searchmatchp[-1:]
for a,l in Base.searchmatchp[-2:]:
  iter1 = sbuffer.get_iter_at_offset(a)
  iter2 = sbuffer.get_iter_at_offset(l)
  sbuffer.select_range(iter1,iter2)
  textview.scroll_to_mark(sbuffer.get_mark('insert'), 0.25)
if Base.searchmatchp ==[]:
  search(self)
于 2013-04-10T02:16:09.823 回答