Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import gtk
- import cairo
- import gobject
- import pangocairo
- import pango
- import time
- class MyWidget(gtk.DrawingArea):
- __gtype_name__ = "MyWidget"
- def __init__(self):
- gtk.DrawingArea.__init__(self)
- self.connect("size-allocate", self.size_allocate_cb)
- self.connect("expose-event", self.do_expose)
- self.set_size_request(320, 240)
- self.set_events(gtk.gdk.ALL_EVENTS_MASK)
- self.show()
- def update():
- self.queue_draw()
- return True
- gobject.timeout_add(100, update)
- def size_allocate_cb(self, widget, allocation):
- self.width = allocation.width
- self.height = allocation.height
- def draw_text(self, cr, text, x, y, width):
- pcr = pangocairo.CairoContext(cr)
- lyt = pcr.create_layout()
- lyt.set_text(text)
- lyt.set_width(pango.units_from_double(width))
- lyt.set_wrap(pango.WRAP_WORD_CHAR)
- cr.move_to(x, y)
- pcr.show_layout(lyt)
- return lyt.get_pixel_size()[1]
- def do_expose(self, widget, event):
- cr = self.window.cairo_create()
- cr.rectangle(0, 0, self.width, self.height)
- cr.set_source_rgba(1, 1, 1, 1)
- cr.fill()
- cr.set_source_rgba(0, 0, 0, 1)
- pcr = pangocairo.CairoContext(cr)
- text_width = 175
- spacing = 50
- text = str(time.time())
- for i in xrange(0, self.width / text_width):
- x = i * text_width
- y = 0
- while y < self.height:
- y += self.draw_text(cr, text, x, y, text_width - spacing) + spacing
- w = gtk.Window()
- w.add(MyWidget())
- w.show()
- gtk.main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement