Log In
New Account
Home My Page Project Cloud Code Snippets Project Openings Python for Maemo
Summary Tracker Lists SCM Wiki

Creating PyGObject-based bindings

This quick tutorial will guide you through the process of creating python bindings for gobject-based libraries.

Quick overview

Usually, to make a C library available from python you use something like ctypes or pyrex, and maybe even write C code manually. While for small functions it's fine to go this way, it can be very hard to get it working with a complex object system like GObject.

That's where we can use PyGTK/GObject bindings and tools. They make most of the dirty work of binding classes and functions to python objects, leaving only some special cases requiring manual intervention.

They work by taking the classes/functions definitions in a .defs file and automatically creating the .c file that will be used to build the extension. When you don't want to use the generated code for a function/method, you only have to write an override for that item, which will be used instead of the generated code.

Defs creation

The .defs file is a piece of scheme code with the definitions of classes, enums, methods and functions from the target library. This information is extracted from .h files with the h2def.py tool provided with the pygtk development package (python2.5-gtk2-dev). It takes the path to the .h files and outputs the scheme code to stdout.

Example of a .defs file content from python-hildon:

 3 (define-object Note
 4   (in-module "Hildon")
 5   (parent "GtkDialog?")
 6   (c-name "HildonNote?")
 7   (gtype-id "HILDON_TYPE_NOTE")
 8 )

In this example, we are defining a Note object, which inherits from a GtkDialog and its C name (structure) is HildonNote.




(last edited December 17, 2007) - Read Only [info] [diff])
FindPage by browsing or searching
5 best incoming links: FrontPage (7), RecentChanges (4)
5 best outgoing links: GtkDialog (2)HildonNote (2)
5 most popular nearby: FrontPage (5788), RecentChanges (783), HildonNote (185), GtkDialog (167)

Terms of Use    Privacy Policy    Contribution Guidelines    Feedback

Powered By GForge Collaborative Development Environment