I wrote a Python version of this C-based article.
http://www.oluyede.org/blog/writing-a-widget-using-cairo-and-pygtk-28/
I wrote a Python version of this C-based article.
http://www.oluyede.org/blog/writing-a-widget-using-cairo-and-pygtk-28/
Additional comments powered by BackType
5 Comments
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
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()
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?
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
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
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
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()
One Trackback/Pingback
[...] 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 [...]