import Pango
...
#Create a new label whose text is rendered by Pango
mylabel = gtk.Label()
mylabel.set_markup("<u>This is pango markup</u> <span foreground=\"orange\">inside a label</span>")
first_page.pack_start(mylabel)
Here's how this label shows up in the UI.

More often than not, you will probably want larger Pango layout areas to display data contained inside of a file rather than data that is conceived within the code of your program. The following code shows a utility method used to read from a file that can then be used to load data in to a Pango layout.
#### Method getFileData, which takes a file_path and returns the data within that file
def getFileData(file_path):
fd = open(file_path, 'r')
try:
data = fd.read()
finally:
fd.close()
return data
With this utility method in place, updating a Pango layout with file data is pretty straightforward:
import pango
...
class TextWidget(gtk.DrawingArea):
def __init__(self):
self.repeat_period_msec = 1000
gtk.DrawingArea.__init__(self)
self.context = None
#create a pango layout. Leave text argument to be empty string since we want to use markup
self.pango_context = self.create_pango_context()
self.pango_layout = self.create_pango_layout('')
...
#### Method update_file, reloads data from filepath and then updates the Pango layout that
# displays the data in the file.
def update_file(self, filepath):
self.pango_layout.set_markup(annotateutils.getFileData(filepath))
self.queue_draw()
Pango Reference Manual
PYGTK's gtk.gdk Class Referencegtk.gdk.PangoRenderer
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/tongxinshuyu/article-51835-6.html
嘴硬是没用的
@老子起个昵称怎么就那么难@郑甜甜的辛普森美美美