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/

Share and Enjoy:
  • Print
  • Digg
  • StumbleUpon
  • Facebook
  • Twitter
  • Google Bookmarks
  • FriendFeed
  • Google Buzz
  • HackerNews
  • Posterous
  • Reddit
  • Slashdot
  • Tumblr
  • http://www.pygtk.org Rafael Villar Burke

    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

  • http://www.rittau.org/ Sebastian Rittau

    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?

  • http://oluyede.org/blog Lawrence

    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 :)

  • http://riffraff.blogsome.com riffraff

    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 ;)

  • http://riffraff.blogsome.com riffraff

    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()

  • Pingback: PDI^2 :: Ruby-GNOME2, i binding dimenticati :: January :: 2006