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?
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 takepango.Attributeand turn convert it into properties that aGtkTextTagcan use.Here’s the function:
This function creates a
MarkupProps()object that has the ability to generateGtkTextTags along with the index in the text to apply them to.Here’s the object:
So with this function and the
MarkupPropsobject, I am able to, given a pango markup string, breakdown the string into it’s properties, and text form, and then convert that intoGtkTextTags.