Newby pango problem
John Hunter
jdhunter at ace.bsd.uchicago.edu
Mon Aug 30 08:54:44 EDT 2004
>>>>> "Friedrich" == Friedrich Dimmling <f.dimmling at remove-this.snafu.de> writes:
Friedrich> Hi, I would like to use pango.layout to display text in
Friedrich> a pygtk2 project. However I could not manage to get the
Friedrich> pango.layout object incorporated in my toplevel
Friedrich> gtk.Window and have it displayed.
Friedrich> If anyone could help me either with a minimal example
Friedrich> like 'Hello World' displayed in italics in an
Friedrich> pango.layout or the URL to a tutorial where I can find
Friedrich> the relevant infos (the PyGtk tutorial seems not to
Friedrich> contain it, unless I'm really blind)
Here is pango_demo.py from my pygtk examples dir. I wrote this a long
time ago, so it may not be the best or most modern usage, but it may
help you along your way
from __future__ import division
import pygtk
pygtk.require('2.0')
import gtk
from gtk import gdk
import pango
win = gtk.Window()
win.show()
vbox = gtk.VBox()
vbox.show()
win.add(vbox)
figsize = 4,4
dpi = 72
def draw(widget):
gc = widget.window.new_gc()
context = widget.create_pango_context()
layout = widget.create_pango_layout('Some Text')
desc = pango.FontDescription('Times 14')
layout.set_font_description(desc)
widget.window.draw_layout(gc, x=100, y=200, layout=layout)
widget.window.draw_rectangle( gc, 1, 100,100,100,100)
def configure_event(widget, event):
global pixmap
pixmap = gtk.gdk.Pixmap(widget.window, 500, 500)
draw(widget)
return gtk.TRUE
def expose_event(widget, event):
draw(widget)
return gtk.TRUE
da = gtk.DrawingArea()
da.connect('configure_event', configure_event)
da.connect('expose_event', configure_event)
da.set_size_request(figsize[0]*dpi, figsize[1]*dpi)
da.show()
vbox.pack_start(da, gtk.TRUE, gtk.TRUE)
def byebye(button):
pixbuf = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, 0, 8,
500,500)
pixbuf.get_from_drawable(pixmap, pixmap.get_colormap(),
0, 0, 0, 0, 500, 500)
pixbuf.save('somefile.png', 'png')
gtk.mainquit()
button = gtk.Button('Quit')
button.show()
vbox.pack_start(button, gtk.TRUE, gtk.TRUE)
button.connect('clicked', byebye)
gtk.mainloop()
More information about the Python-list
mailing list