0

gtk.TextViewマークアップのようなテキストを追加したい があります。gtk.TextTagこれは、pango マークアップ文字列と同様のプロパティで作成できるものを使用することで実現できることを知っています。gtk.TextBuffer他の複数のウィジェットでできるように、 set_markup を単に言う簡単な方法がないことに気付きました。代わりに、TextTag を作成し、プロパティを指定してから、タグが適用される iter を指定して 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()を生成する機能を持つオブジェクトを作成します。GtkTextTag

オブジェクトは次のとおりです。

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'])

したがって、この関数とオブジェクトを使用すると、pango マークアップ文字列を指定して、文字列をそのプロパティとテキスト形式に分解し、それをsMarkupPropsに変換できます。GtkTextTag

于 2012-03-27T21:20:49.437 に答える
1

GTK +の開発をフォローしていません。最近何かを追加した可能性がありますが、これらのバグを参照してください:#59390および#505478。それらは閉じられていないので、おそらく何も行われません。

于 2012-03-18T12:37:23.620 に答える