Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7697751
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T22:05:04+00:00 2026-05-31T22:05:04+00:00

I’ve got a gtk.TextView that I’d like to add markup-like text to. I know

  • 0

I’ve got a gtk.TextView that I’d like to add markup-like text to. I know this can be achieved through the use of gtk.TextTag which you can create with similar properties as a pango markup string. I noticed there is no easy way to just say set_markup to a gtk.TextBuffer much like you can with multiple other widgets. Instead you have to create a TextTag, give it properties, and then insert it into the TextBuffer’s TagTable specifying the iters that the tag applies to.

I’d ideally like to create a function that can convert a pango markup string into a TextTag to get the same effect. But gtk doesn’t appear to have that functionality built-in.
I’ve noticed that you can use pango.parse_markup() on a marked up string and it will create a pango.AttributeList which contains information regarding the properties set on the string and the indices that they occur at. But there are slight differences in each type of attribute that make it difficult to generalize for every case. Is there a better way to go about this? Or is pango markup just not meant to be converted into gtk.TextTag‘s?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T22:05:05+00:00Added an answer on May 31, 2026 at 10:05 pm

    I finally worked out my own solution to this problem. I created a function that parses the markup string (using pango.parse_markup). Through reading the documentation and python introspection, I was able to work out how to take pango.Attribute and turn convert it into properties that a GtkTextTag can use.

    Here’s the function:

    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
    

    This function creates a MarkupProps() object that has the ability to generate GtkTextTags along with the index in the text to apply them to.

    Here’s the object:

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

    So with this function and the MarkupProps object, I am able to, given a pango markup string, breakdown the string into it’s properties, and text form, and then convert that into GtkTextTags.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I know there's a lot of other questions out there that deal with this
Does anyone know how can I replace this 2 symbol below from the string
i got an object with contents of html markup in it, for example: string
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.