0

嗨,我正在制作一个程序,它可以将不同的属性应用于使用 iters 在文本小部件中键入的文本。然后根据迭代将该文本拆分并传递给外部程序。

但是,我只能获得应用的第一个迭代器,而无法获得任何后续迭代器。

例如。(粗体)你好(粗体)(斜体)你好吗(斜体)

其中(粗体)和(斜体)代表迭代。然而,下面的代码仅识别 (Bold)Hi there(Bold) 并将 (bold) 存储在 tagList 和 Hi there 在 textList 中。将标签名称附加到 tagList 时会引发列表索引超出范围错误。有没有办法解决这个问题?

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
4

1 回答 1

1

解决它!forward_to_tag_toggle 需要调用两次,因为它首先跳转到标签的关闭切换。再次调用它会跳转到下一个标签的 on toggle

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
于 2012-10-21T04:13:36.887 回答