0

我有一个gtk.TextView我想添加类似标记的文本。我知道这可以通过使用gtk.TextTag它来实现,您可以使用与 pango 标记字符串类似的属性来创建它。我注意到没有简单的方法可以gtk.TextBuffer像使用多个其他小部件一样说 set_markup。相反,您必须创建一个 TextTag,为其提供属性,然后将其插入到 TextBuffer 的 TagTable 中,指定标签适用的迭代器。

理想情况下,我想创建一个可以将 pango 标记字符串转换为 TextTag 以获得相同效果的函数。但是 gtk 似乎没有内置该功能。我注意到您可以pango.parse_markup()在标记的字符串上使用它,它将创建一个pango.AttributeList包含有关在字符串上设置的属性和它们出现的索引的信息。但是每种类型的属性都有细微的差异,因此很难对每种情况进行概括。有没有更好的方法来解决这个问题?还是 pango 标记只是不打算转换为gtk.TextTag's?

4

2 回答 2

2

我终于找到了自己的解决方案来解决这个问题。我创建了一个解析标记字符串的函数(使用pango.parse_markup)。通过阅读文档和 python 自省,我能够弄清楚如何pango.Attribute将其转换为GtkTextTag可以使用的属性。

这是功能:

def parse_markup_string(string):
    '''
    Parses the string and returns a MarkupProps instance
    '''
    #The 'value' of an attribute...for some reason the same attribute is called several different things...
    attr_values = ('value', 'ink_rect', 'logical_rect', 'desc', 'color')

    #Get the AttributeList and text
    attr_list, text, accel = pango.parse_markup( string )
    attr_iter = attr_list.get_iterator()

    #Create the converter
    props = MarkupProps()
    props.text = text

    val = True
    while val:
            attrs = attr_iter.get_attrs()

            for attr in attrs:
                    name = attr.type
                    start = attr.start_index
                    end = attr.end_index
                    name = pango.AttrType(name).value_nick

                    value = None
                    #Figure out which 'value' attribute to use...there's only one per pango.Attribute
                    for attr_value in attr_values:
                            if hasattr( attr, attr_value ):
                                    value = getattr( attr, attr_value )
                                    break

                    #There are some irregularities...'font_desc' of the pango.Attribute
                    #should be mapped to the 'font' property of a GtkTextTag
                    if name == 'font_desc':
                            name = 'font'
                    props.add( name, value, start, end )

            val = attr_iter.next()

    return props

此函数创建一个MarkupProps()对象,该对象能够生成GtkTextTags 以及文本中的索引以应用它们。

这是对象:

class MarkupProps():
'''
Stores properties that contain indices and appropriate values for that property.
Includes an iterator that generates GtkTextTags with the start and end indices to 
apply them to
'''
def __init__(self): 
    '''
    properties = (  {   
                        'properties': {'foreground': 'green', 'background': 'red'}
                        'start': 0,
                        'end': 3
                    },
                    {
                        'properties': {'font': 'Lucida Sans 10'},
                        'start': 1,
                        'end':2,

                    },
                )
    '''
    self.properties = []#Sequence containing all the properties, and values, organized by like start and end indices
    self.text = ""#The raw text without any markup

def add( self, label, value, start, end ):
    '''
    Add a property to MarkupProps. If the start and end indices are already in
    a property dictionary, then add the property:value entry into
    that property, otherwise create a new one
    '''
    for prop in self.properties:
        if prop['start'] == start and prop['end'] == end:
            prop['properties'].update({label:value})
    else:
        new_prop =   {
                        'properties': {label:value},
                        'start': start,
                        'end':end,
                    }
        self.properties.append( new_prop )

def __iter__(self):
    '''
    Creates a GtkTextTag for each dict of properties
    Yields (TextTag, start, end)
    '''
    for prop in self.properties:
        tag = gtk.TextTag()
        tag.set_properties( **prop['properties'] )
        yield (tag, prop['start'], prop['end'])

因此,有了这个函数和MarkupProps对象,我可以给定一个 pango 标记字符串,将字符串分解为其属性和文本形式,然后将其转换为GtkTextTags。

于 2012-03-27T21:20:49.437 回答
1

没有关注 GTK+ 的开发,也许他们最近添加了一些东西,但是看到这些错误:#59390#505478。由于它们没有关闭,因此可能什么也没做。

于 2012-03-18T12:37:23.620 回答