misterMan
misterMan

Reputation: 159

GTK getting iter names and the text it is applied to

Hi I am making a program which can apply different properties to text typed in a text widget using iters. This text is then split according to the iters and passed to an external program.

However, I can only get the first iter that is applied, and cant get any subsequent iters.

eg. (Bold)Hi there(Bold) (Italic)How are you(Italic)

where (Bold) and (Italic) represent iters. The code below however, only recognizes (Bold)Hi there(Bold) and stores (bold) in tagList and Hi there in textList. It throws a list index out of range error when appending the tag name into tagList. Is there anyway to fix this problem?

def splitTextMarkup(self):
    it =buff.get_start_iter()
    tagList=[]
    textList=[]
    while not it.is_end(): 
        nextpos=it.copy()
        nextpos.forward_to_tag_toggle(None)
        textList.append(it.get_text(nextpos))
        tagList.append(it.get_tags()[0].props.name)
        it=nextpos
    return tagList,textList

Upvotes: 0

Views: 163

Answers (1)

misterMan
misterMan

Reputation: 159

Fixed it! the forward_to_tag_toggle needs to be called twice as it jumps to the off toggle of the tag first. Calling it again makes it jump to the on toggle of the next tag

def splitTextMarkup(self):
it =buff.get_start_iter()
tagList=[]
textList=[]
while not it.is_end(): 
    nextpos=it.copy()
    nextpos.forward_to_tag_toggle(None)
    nextpos.forward_to_tag_toggle(None)
    textList.append(it.get_text(nextpos))
    tagList.append(it.get_tags()[0].props.name)
    it=nextpos
return tagList,textList

Upvotes: 1

Related Questions