Find text in gtk.TextView

I have gtk.Textview . I want to find and select a piece of text in this TextView programmatically. I have this code, but it does not work 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 to select it only the found text.

+4
source share
1 answer

start_iter.forward_search returns a tuple of start and end matches, so your found variable has 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) 
+4
source

Source: https://habr.com/ru/post/1302877/


All Articles