Monday, April 2, 2012

python gtk 3+ example implementing cairo drawn object


from gi.repository import Gtk, GdkPixbuf, Gdk
import os, sys
import math



class GUI:
    def draw_callback(guiobj, widget, cr):
            def MIN(x, y):
                if x < y:
                    return x
                else:
                    return y
               
        width = 100
        height = 100
       
        ##cr = Gdk.cairo_create()
##        width = widget.get_allocation().x
##        height = widget.get_allocation().y
        cr.arc (
                 width / 2.0, height / 2.0,
                 MIN (width, height) / 2.0,
                 0, 2 * math.pi)

        stylecontext = widget.get_style_context()                
        color = stylecontext.get_color (0)
        cr.set_source_rgba (0.5, 0.5, 0.5)
       
        cr.fill()
                
        return FALSE
       
       
    def __init__(self):
       
            window = Gtk.Window()
            window.set_title ("Hello World")
            box = Gtk.Box('Vertical',5)
           
            self.drawing_area = Gtk.DrawingArea()
            self.drawing_area.set_size_request (100, 100)
            box.pack_start(self.drawing_area,
                           True,
                           True,
                           0)
           
            self.button = Gtk.Button('Hello')
            box.pack_start(self.button,
                           True, True, 0)
            window.add(box)
                       
            self.drawing_area.connect('draw', self.draw_callback)
            window.connect_after('destroy', self.destroy)


            window.show_all()

    def destroy(window, self):
            Gtk.main_quit()

def main():
    app = GUI()
    Gtk.main()
       
if __name__ == "__main__":
    sys.exit(main())

So I managed to translate a gtk3+ example into python calls,
this is done outside graphic ide builder (e.g., Glade or visual Anjuta),
 here I've managed to use for a drawn graphic context using Cairo, to implement via a Drawing Area widget object.  Since in gtk3+ 'expose event' has been removed for drawing in this widget, instead a drawing event can be handled using a 'draw' event handler.  The function 'draw_callback' implements as in the gtk3+ example a pythonic version here.   Automatically this event sends three arguments to the draw_callback function in the named ordering at the event functions header declaration....note a bit different here relative to c gtk structure.  'cr' designates the automatically created 'cairo context' which is later used to draw cairo based objects...although I hadn't fully implemented as in the example, either grabbing the width height of the drawing area object widget, or the color return from the widget's style context...this should be a three tuple rgb assignment in order to set this properly with a python based cairo function call.

Original c code example here

No comments:

Post a Comment

Oblivion

 Between the fascination of an upcoming pandemic ridden college football season, Taylor Swift, and Kim Kardashian, wildfires, crazier weathe...