from gi.repository import Gtk, Gdk, cairo, Pango, PangoCairo import math import sys RADIUS = 150 N_WORDS = 10 FONT = "Sans Bold 27" class Squareset(Gtk.DrawingArea): def __init__ (self, upper=9, text=''): ## Gtk.Widget.__init__(self) Gtk.DrawingArea.__init__(self) self.set_size_request (200, 200) ## self.show_all() def do_draw_cb(self, widget, cr): # The do_draw_cb is called when the widget is asked to draw itself # with the 'draw' as opposed to old function 'expose event' # Remember that this will be called a lot of times, so it's usually # a good idea to write this code as optimized as it can be, don't # Create any resources in here. cr.translate ( RADIUS, RADIUS) layout = PangoCairo.create_layout (cr) layout.set_text("Text", -1) desc = Pango.font_description_from_string (FONT) layout.set_font_description( desc) ## desc.free() rangec = range(0, N_WORDS) for i in rangec: width, height = 0,0 angle = (360. * i) / N_WORDS; red =0.0 cr.save () ## #/* Gradient from red at angle == 60 to blue at angle == 240 */ red = (1 + math.cos ((angle - 60) * math.pi / 180.)) / 2 cr.set_source_rgb ( red, 0, 1.0 - red) cr.rotate ( angle * math.pi / 180.) #/* Inform Pango to re-layout the text with the new transformation */ PangoCairo.update_layout (cr, layout) width, height = layout.get_size() cr.move_to ( - (float(width) / 1024.) / 2, - RADIUS) PangoCairo.show_layout (cr, layout) cr.restore() def destroy(window): Gtk.main_quit() def main(): window = Gtk.Window() window.set_title ("Hello World") box = Gtk.Box('Vertical',5) app = Squareset() ## box.pack_start(app, ## True, ## True, ## 0) window.add(app) app.connect('draw', app.do_draw_cb) window.connect_after('destroy', destroy) window.show_all() Gtk.main() if __name__ == "__main__": sys.exit(main())Translated C api example to python utilising PangoCairo and Pango libraries.
This draws pango cairo text
Original example in C here
Hi Christopher
ReplyDeleteThis code doesn't work in my Ubuntu 12.10. The callback of the event "draw" is never called.
This post was generated sometime ago, technically at the time that I worked with my particular Ubuntu build version it worked. Unfortunately, not sure if the particularly libraries have changed (I'd mention that these libraries have changed enough from gtk2+ to 3+ and this post was generated immediately after conversion and that subsequently I've witnessed some handling procedure changes), not sure what references might be used at this point. At the time if you could read C enough you might be able to follow some of the updated procedures/methods/function calls at the GNOME/gtk reference sites and figure out the pythonic versions on your own...or you could use python itself to explore the gtk libraries (been awhile since I've done this although I think I have a post referenced at my blog site that covers some of the explorations aspects here).
ReplyDeleteTo Kr0mAgn0n and others who can't get their draw events to fire, you're probably missing the python-gi-cairo package. Install it with Synaptic or apt-get-install and re-run your script. The draw events should now fire as you expect. There's no need to import another library. Info from here: http://www.hellonull.com/?p=118
ReplyDeleteThanks a lot for your python translation!
ReplyDelete