Skip to content

Writing a Widget Using Cairo and PyGTK 2.8

I wrote a Python version of this C-based article.

http://www.oluyede.org/blog/writing-a-widget-using-cairo-and-pygtk-28/

5 Comments

  1. Nice article!. This article is now included (as a link to your blog) in the articles section in the pygtk.org website. We’d like to have a copy of it too in our site just in case your blog changes its address or it gets unavaliable. Would you mind it?.

    Thanks for your work and letting others enjoy it.

    Rafael

    Monday, December 5, 2005 at 12:12 pm | Permalink
  2. Great to see that you converted this tutorial to Python. Many thanks! Here are some questions/suggestions, though:

    def init(self): gtk.DrawingArea.init(self)

    I would recommend to use the super operator:

    def init(self): super(EggClockFace, self).init()


    rect = self.get_allocation() x = rect.x + rect.width / 2 y = rect.y + rect.height / 2

    It’s probably better to make sure (for now) that floating point numbers are used for the division:

    rect = self.get_allocation() x = rect.x + rect.width / 2.0 y = rect.y + rect.height / 2.0

    Or if you want to force an integer division:

    rect = self.get_allocation() x = rect.x + rect.width // 2 y = rect.y + rect.height // 2


    def expose(self, widget, event): self.context = widget.window.cairo_create()

      self.draw(self.context)
    

    Well, self.draw is actually an inherited method from gtk.Widget. I would recommend to rename that method to draw_face or something similar.

    Also, why do you store the context in a member?

    Tuesday, December 6, 2005 at 3:23 am | Permalink
  3. Lawrence wrote:

    Thanks for the reply Sebastian!

    1 – there’s no compelling reason to use super() instead of the old method in a single inheritance situation (anyway I corrected)

    2 – ok for the floating point division

    3 – draw() is an explicit override

    4 – self.context is just a matter of style, I wrote the conversion quickly and this should be also a mistake :)

    Tuesday, December 6, 2005 at 11:59 am | Permalink
  4. riffraff wrote:

    I know you were trying to redo this in ruby.. I got it working with this:

    require ‘cairo’ require ‘gtk2′

    class EggClockFace

    by installing first rcairo-1.0.0 and then ruby-gnome2-0.14.1 from source on ubuntu breezy (and after installing a boatload of xyz-dev packages with apt). The code could be more rubyified, anyway, but my box is dead ATM.

    No time to blog it, though ;)

    Wednesday, December 7, 2005 at 4:15 pm | Permalink
  5. riffraff wrote:

    emh.. code got deleted: require ‘cairo’ require ‘gtk2′

    class EggClockFace < Gtk::DrawingArea def initialize super self.redraw_on_allocate=true signal_connect “expose_event” do |widget,event| ctx= widget.window.create_cairo_context ctx.rectangle(event.area.x, event.area.y, event.area.width, event.area.height) ctx.clip draw(ctx) false end end def draw(ctx) rect = allocation() x = rect.x + rect.width / 2.0 y = rect.y + rect.height / 2.0

        radius = [rect.width / 2.0, rect.height / 2.0].min - 5
    
        # clock back
        ctx.arc(x, y, radius, 0, 2.0 * Math::PI)
        ctx.set_source_rgb(1, 1, 1)
        ctx.fill_preserve()
        ctx.set_source_rgb(0, 0, 0)
        ctx.stroke()
    
        for i in 0..11
            inset = 0.1 * radius
    
            ctx.move_to(x + (radius - inset) * Math.cos(i * Math::PI / 6.0),
                            y + (radius - inset) * Math.sin(i * Math::PI / 6.0))
            ctx.line_to(x + radius * Math.cos(i * Math::PI / 6.0),
                            y + radius * Math.sin(i * Math::PI / 6.0))
            ctx.stroke()
        end
        ctx.stroke()
    

    end end

    Gtk.init window = Gtk::Window.new clock = EggClockFace.new

    window.add(clock) window.signal_connect “destroy” do Gtk.main_quit end window.show_all()

    Gtk.main()

    Wednesday, December 7, 2005 at 4:17 pm | Permalink

One Trackback/Pingback

  1. PDI^2 :: Ruby-GNOME2, i binding dimenticati :: January :: 2006 on Thursday, January 12, 2006 at 5:08 pm

    [...] Colgo l’occasione invece per fare un port dell’applicazione che aveva tradotto lawrence da C a python, per mostrare come usare insieme Cairo e Gtk. Io ho fatto le prove su ubuntu breezy badger e su win32 e funzionano, per altre distro non garantisco [...]

Additional comments powered by BackType