Wednesday, April 4, 2012

Python conversion notes between gtk2+ and gtk3+

Some recent tidbits on translations between C and and python languages in so far as gtk libraries.

So far some fairly easy to discern conversions:

Often in python a an object will have a method call which is done so in terms of the convention

gtk.object.method_call

whereas at least in gtk libraries, by c convention,

gtk_object_method_call(object,...)

so if hadn't had a readily provided api, you might be able to guess the method call simply altering syntax here.  Helps also to use the python console to verify existence firstly...as mentioned before a dir(object) call can provide method information alongside additional argument calls for such object.  In c a bit more verbose and redundant then python here with respect to naming conventions on method calls and the applicable class objects to them therein but that's the way it is...

Additionally, some things that I have come across...

type flag objects and the like in the form

gtk.KEYNAME_OBJECTFLAG

need to be converted, not just in gdk libraries.

I believe this were partially (if not fully) mentioned in the conversion process for the C api.

typically something OBJECT might be expressed as

gtk.KEYNAME_OBJECTFLAG

instead, in python the conversion might look like

Gtk.KeyName.OBJECTFLAG

the C api however generally helps in discerning the KeyName class object call.  Ways to find this in the api are:

1.  Probe the method call utilising such flag, the API may provide KeyName
     here even if in gtk2+ it appears differently(truncated or otherwise) relative
     to gtk3+.
2.  In c typically it may be written GtkKeyName (something like
     this anyways)...here you can run the command in python console
     dir(Gtk.KeyName), and you may find a directory of flag objects
     for the given KeyName. 
3.  Obvious the python syntax places a '.' between 'Gtk' and 'KeyName'
     and typically in python possibly converting  '_' to '.'  

generally speaking similarity in method call structures and objects appear to abound cross gtk2+ and gtk3+ libraries, so elsewhere conversion appears fairly straight forward.

As a translating research example I have the following window flag conversion:

w = gtk.Window()
....
w.set_type_hint(gtk.WINDOW_POPUP)

as it turns out we partially have in gtk3+ KeyName provided but not the whole story here and the flag is obtained through a different library altogather, namely, gdk.   How to start here:

1.  we notice the 'w' object is a gtk.Window() object
2.  Then we notice 'set_type_hint'  is a method call on the gtk_window class object
3.  We look up the method call 'set_type_hint' on gtk_window class object in the C api.  where we find that in our look up the following is yielded:

void                gtk_window_set_type_hint            (GtkWindow *window,
                                                         GdkWindowTypeHint hint);

Thus it appears that the argument flag call object for this method in gtk3+ is a
GdkWindwTypeHint object.

At console with Gdk library imported we can call

>> dir(Gdk.WindowTypeHint)

where a list of flags are populated confirming our desired flag.

As it turns out the converted call in gtk3+  is

Gdk.WindowTypeHint.POPUP_MENU

which is the closest equivalent.

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...