[pygtk] Find string within TextBuffer

Steve McClure smcclure at racemi.com
Fri Mar 13 21:30:15 WST 2009


On Mar 13, 2009, at 8:23 AM, Steve McClure wrote:

>
> On Mar 13, 2009, at 6:52 AM, Lupine wrote:
>
>> Hello all,
>>
>> I've read over:
>> http://www.pygtk.org/pygtk2tutorial/sec-TextBuffers.html
>> http://www.pygtk.org/pygtk2tutorial/sec-TextIters.html
>> ...but I'm not finding what I'm looking for, which is probably really
>> simple for someone out there.  If you can point me to another  
>> tutorial
>> were this is covered, or example code..either would be much
>> appreciated.
>>
>> I have a simple Python/PyGTK/Glade application, which has a
>> gtk.TextView
>> that contains some text from the gtk.TextBuffer.  Upon the click of
>> the
>> "find" button, how can I scroll through each line of the buffer
>> looking
>> for the entered string.  This is what I got so far:
>>
>> def on_find_dialog_ok_button_clicked(self, action):
>> """This will highlight each found entry"""
>> findentry = self.widgets.get_widget('find_entry').get_text()
>> txtbuffer =  
>> self.widgets.get_widget('pref_hosts_txtview').get_buffer()
>> startiter = txtbuffer.get_start_iter()
>> enditer = txtbuffer.get_end_iter()
>> bufferlines = txtbuffer.get_text(startiter, enditer)
>
> I think the iterator points to characters in a string, not strings in
> a list.  If so you would just need something like:
>
> bufferstr = txtbuffer.get_text(startiter, enditer)
> if findentry in bufferstr:
>    print 'found entry:'

Or how about:

start = txtbuffer.get_start_iter()
first, last = start.forward_search(findentry)
if first:
     print 'found entry'

>
>> for bufferline in bufferlines:
>>  print "looking at " + bufferline
>>    if (re.search(findentry, bufferline)):
>>      print "found entry: " + bufferline
>> print "all done looking"
>>
>> The result, is that it is printing each character (one char at a  
>> time)
>> on each separate line, which explains why my search is failing.  So
>> what
>> is the proper way to look through each line for str?
>>
>> Thanks,
>> -Lup
>>
>> _______________________________________________
>> pygtk mailing list   pygtk at daa.com.au
>> http://www.daa.com.au/mailman/listinfo/pygtk
>> Read the PyGTK FAQ: http://faq.pygtk.org/
>>
>>
>>
>
> --
> Steve McClure
> smcclure at racemi.com
>
> _______________________________________________
> pygtk mailing list   pygtk at daa.com.au
> http://www.daa.com.au/mailman/listinfo/pygtk
> Read the PyGTK FAQ: http://faq.pygtk.org/
>

--
Steve McClure
smcclure at racemi.com



More information about the pygtk mailing list