0xAX
0xAX

Reputation: 21817

Find text in gtk.TextView

I have a gtk.Textview. I want to find and select some of the text in this TextView programmatically. I have this code but it's not working correctly.

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)

If the text is found, then it selects all the text in the TextView, but I need it to select only the found text.

Upvotes: 4

Views: 2746

Answers (1)

John
John

Reputation: 584

start_iter.forward_search returns a tuple of the start and end matches so your found variable has both match_start and match_end in it

this should make it work:

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)

Upvotes: 6

Related Questions