[disclaimer]


This is a personal blog. The opinions expressed here represent my own and not those of any of my employers or customers.

Except if stated otherwise, all the code shared is reusable under a MIT/X11 licence. If a picture is missing a copyright notice, it's probably because I'm owning it.

Friday, August 28, 2009

GtkBuilder on IronPython

Someone asked me if I could add the missing parts of GtkBuilder in Gtk#Beans so he could use it with IronPython on mono.

Hey, it looks there's no missing parts ! It all works fine since day one. Here's the the trick:

import clr
clr.AddReference('glib-sharp')
clr.AddReference('gtk-sharp')
clr.AddReference('gtk-sharp-beans')
import Gtk
import GLib
import GtkBeans
import System.IO

def PyBuilderAutoconnect(builder, target):
def _connect(builder, object, signal_name, handler_name, connect_object, flags):
name = ''.join([frag.title() for frag in signal_name.split('_')])
event = getattr(object, name)
event += getattr(target, handler_name)

for object in builder.Objects:
setattr(target, object.Name, object)
builder.ConnectSignalsFull (_connect)

class Application:
def __init__(self):
builder = GtkBeans.Builder (System.IO.FileStream ('ui.ui', System.IO.FileMode.Open))
#use this ctor if you don't like FileStream
#builder = GtkBeans.Builder ()
#builder.AddFromFile ('./ui.ui')

PyBuilderAutoconnect (builder, self)
self.window1.ShowAll ()

def onbuttonclicked(self, o, args):
Gtk.Application.Quit()

Gtk.Application.Init ()
app = Application ()
Gtk.Application.Run ()
Now your IronPython skills are ready to rock Gnome3!

1 comment: