GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Wed Oct 22, 2014 11:57 pm

All times are UTC




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: [Solved]Python get iters for specific occurrance in textbuff
PostPosted: Fri Aug 15, 2014 7:27 pm 
Offline
Familiar Face

Joined: Tue Oct 08, 2013 5:16 am
Posts: 20
I'm trying to get the iters for a specific occurrance of a word in a textbuffer. However, if the word occurs more than once in the buffer, how do I know if I'm getting the iters for the specific occurrance of the word that I want?
In the following code, I have a function that steps through a buffer one word at a time, and a function that gets the selection bounds of the word. Again, how do I know if I'm getting the selection bounds for the correct occurrance of the word, if the word occurs more than once in the buffer?
Code:
    def split_buffer(self, buff):
        """Step through buffer one word at a time"""
        try:
            buff = re.split('\s', buff, maxsplit=1)
            word, buff = buff[0], buff[1]
        except:
            word, buff = buff[0], None
        return word, buff

    def get_text_iters(self, buff, word):
        """Get iters for word"""
        return self.buff.get_selection_bounds(word)


Last edited by taupist on Thu Aug 21, 2014 1:43 am, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: Python get iters for specific occurrance of word in text
PostPosted: Sun Aug 17, 2014 7:37 pm 
Offline
Familiar Face

Joined: Thu Aug 20, 2009 1:54 pm
Posts: 42
Location: Belgium
It depends which occurrence you want… The first one? The last one? The one just after or before the cursor? The first one visible on the screen? …


Top
 Profile  
 
 Post subject: Re: Python get iters for specific occurrance of word in text
PostPosted: Tue Aug 19, 2014 2:20 am 
Offline
Familiar Face

Joined: Tue Oct 08, 2013 5:16 am
Posts: 20
That is what I was asking; WHICH occurrance am I getting, HOW do I know?


Top
 Profile  
 
 Post subject: Re: Python get iters for specific occurrance of word in text
PostPosted: Tue Aug 19, 2014 8:32 pm 
Offline
Familiar Face

Joined: Sat Jul 26, 2014 11:43 pm
Posts: 46
Maybe something like this? Get the iters and index your matches or something like that?

Code:
#!/user/bin/python

from gi.repository import Gtk

class TextBox(Gtk.TextView):
    def __init__(self):
        Gtk.TextView.__init__(self)
        self.set_wrap_mode(2)
        self.textbuffer = self.get_buffer()
        self.textbuffer.set_text("Find a word word word word1 word1 word2 word2 word2")
   
    def get_word(self, text1): ##text1 is word to match
        offset = 0
        prev = 0
        move = 0
        index = 1
        find_match = True
        start1 = self.textbuffer.get_start_iter()
        end1 = self.textbuffer.get_end_iter()
        print("Chars " + str(end1.get_offset()))
        if(text1):
            while find_match:
                match1 = start1.forward_search(text1, 0, None)
                if(match1):
                    print(str(index) + " " + self.textbuffer.get_slice(match1[0], match1[1], include_hidden_chars=True) + " " + str(match1[0].get_offset()) +"-" + str(match1[1].get_offset()))           
                    offset = match1[1].get_offset()
                    move = offset - prev
                    prev = offset
                    ##Move start1 iter forward.
                    start1.forward_chars(move)
                    index = index + 1
                else:
                    find_match=False
        else:
            print("Empty Entry.")

class MainWindow(Gtk.Window):
    def __init__(self):
        Gtk.Window.__init__(self, title="Text Iters")
        self.set_default_size(400,400)
        self.TextBox1 = TextBox()
        self.TextBox1.set_hexpand(True)
        self.TextBox1.set_vexpand(True)
        self.button1 = Gtk.Button("Find Words")
        self.button1.connect("clicked", self.MatchWord)
        self.entry1 = Gtk.Entry()
        self.entry1.set_hexpand(True)
        self.grid = Gtk.Grid()
        self.grid.attach(self.button1, 0, 0, 1, 1)
        self.grid.attach(self.entry1, 1, 0, 1, 1)
        self.grid.attach(self.TextBox1, 0, 2, 2, 1)
        self.add(self.grid)

    def MatchWord(self, button):
        text1 = self.entry1.get_text()
        self.TextBox1.get_word(text1)

win = MainWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()


Top
 Profile  
 
 Post subject: Re: Python get iters for specific occurrance of word in text
PostPosted: Thu Aug 21, 2014 1:40 am 
Offline
Familiar Face

Joined: Tue Oct 08, 2013 5:16 am
Posts: 20
cecashon, thanks for the reply. That's REALLY close to what I'm looking for, although I'm going to have to study it for a day or two. I think I can make it work. Thank you again.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC


Who is online

Users browsing this forum: Yahoo [Bot] and 5 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group