2010-03-02 7 views
4

J'ai un gtk.Textview. Je veux trouver et sélectionner une partie du texte dans ce TextView par programme. J'ai ce code mais ça ne fonctionne pas correctement.Rechercher du texte dans gtk.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) 

Si le texte se trouve, il sélectionne tout le texte dans le TextView, mais j'ai besoin de sélectionner uniquement le texte trouvé.

Répondre

4

start_iter.forward_search retourne un tuple de début et de fin correspond donc votre variable found a à la fois match_start et match_end dans ce

cela devrait le faire fonctionner:

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) 
+0

Merci pour la réponse, vous vraiment me aidais ! – 0xAX

Questions connexes