Python 在gtk.TextView中查找文本

Python 在gtk.TextView中查找文本,python,gtk,pygtk,Python,Gtk,Pygtk,我有一个gtk.Textview。我想以编程方式查找并选择此TextView中的一些文本。 我有这个代码,但它不能正常工作 search_str = self.text_to_find.get_text() start_iter = textbuffer.get_start_iter() match_start = textbuffer.get_start_iter() match_end = textbuffer.get_end_iter() found = star

我有一个
gtk.Textview
。我想以编程方式查找并选择此
TextView
中的一些文本。 我有这个代码,但它不能正常工作

search_str =  self.text_to_find.get_text()
start_iter =  textbuffer.get_start_iter() 
match_start = textbuffer.get_start_iter() 
match_end =   textbuffer.get_end_iter() 
found =       start_iter.forward_search(search_str,0, None) 
if found: 
   textbuffer.select_range(match_start,match_end)

如果找到文本,则它会选择
TextView
中的所有文本,但我需要它只选择找到的文本。

start\u iter.forward\u search
返回开始和结束匹配的元组,以便
found
变量中既有
match\u start
又有
match\u end

这将使它发挥作用:

search_str =  self.text_to_find.get_text()
start_iter =  textbuffer.get_start_iter()
# don't need these lines anymore
#match_start = textbuffer.get_start_iter() 
#match_end =   textbuffer.get_end_iter() 
found =       start_iter.forward_search(search_str,0, None) 
if found:
   match_start,match_end = found #add this line to get match_start and match_end
   textbuffer.select_range(match_start,match_end)

谢谢你的回复,你真的帮助了我!