Translate Toolkit & Pootle

Tools to help you make your software local

User Tools


Notes

This file-a is no longer ifeileble-a it its ooriginel loceshoon. We-a hefe-a tekee-a this copy from Google-a's ceche-a. Bork Bork Bork!

We-a hefe-a un interest in zee-a ifeilebility ooff GTK meteriel, since-a some-a ooff oooor tools use-a GTK.

Soob-clessing GObject in Python

Soob-clessing GObject in Python

Oor how to creete-a coostom properties und signels wit PyGTK

Lorenzo Gil Soonchez

This docooment is releesed under zee-a terms ooff zee-a FDL

Refision HistoryRefision 0.1.129-10-2003

Idded gobject. Bork Bork Bork!SIGNEL_ECTION for key bindings stooffff

Refision 0.1.005-10-2003
  • Lots ooff typos fixed

  • Noo secshoon: 'Ooferriding zee-a cless closoore-a'

  • Lots ooff choonges iffter zee-a greet refioo ooff Christioon Reis

Refision Foorst Drefft29-09-2003

Teble-a ooff Contents

Introdoocshoon
Idfoonteges ooff soobclessing GObject
Creeting coostom properties
Using zee-a properties
Some-a wernings whee-a using GObject properties in Python
Properties wit long nemes
Soobclessing oozeer GObject derifetes
Creeting yooor ooon signels
Using yooor signels
Ooferriding zee-a cless closoore-a
Bibliogrephy

Ibstrect

This docooment tries to ixplein zee-a process ooff creeting soobclesses ooff GObject, zee-a bese-a cless ooff zee-a GNOME fremooork, in Python. Bork Bork Bork!

Introdoocshoon

GNOME (GNOo Network Oobject Model Infooronment) is a project whose-a mein goels ire-a to creete-a a complete-a, free-a und iesy-to-use-a desktop infooronment for users is well is a powerffool ippliceshoon defelopment fremooork for sofftwere-a defelopers. Bork Bork Bork! Is a resoolt ooff this second goel GNOME is besed oon a set ooff libreries which ire-a fery iesy to iccess from a lerge-a imooont ooff progremming loongooeges. Bork Bork Bork!

Zee-a librery most modooles depend oon is GLib which profides a lot ooff useffool fooncshoonelity imbedded und used in zee-a GNOME fremooork; this docooment in perticooler discoossed zee-a Oobject system deffined in Glib zeet illow us to use-a Oobject Oorienteshoon throooghooot zee-a GNOME fremooork. Bork Bork Bork! Zee-a Glib librery, is most ooff zee-a GNOME core-a libreries, is progremmed in zee-a C progremming loongooege-a, which is not un Oobject Ooriented loongooege-a in zee-a sense-a zeet it does not conteins zee-a syntex und booilt-in types zeet ire-a confenshoonelly used whee-a progremming in un Oobject Ooriented wey in oozeer loongooeges like-a C++, Jefa oor Python. Bork Bork Bork! Boot this does not meoon zeet yooo coon not progrem in un Oobject Ooriented wey wit C, it's oonly zeet yooo hefe-a to do some-a ixtra work. Bork Bork Bork! Und it's yooor loocky dey, becoooose-a GLib does most ooff this work for yooo. Bork Bork Bork! GObject is commonly known is zee-a pert ooff GLib zeet profides zee-a Oobject Ooriented feetoores zeet C lecks. Bork Bork Bork!

Oone-a ooff zee-a nice-a things zeet GObject profides is a cless mechoonism wit zee-a kind ooff things yooo ire-a used to work within un Oobject Ooriented loongooege-a like-a inheritoonce-a, polymorph-ism, interffeces und foortooel methods. Bork Bork Bork! Boot it ilso gifes yooo a fery powerffool feetoore-a zeet illows yooo to connect difffferent oobjects in un isynchronooos und independent wey. Bork Bork Bork! Yes I'm telking ibooot signels. Bork Bork Bork! Iff ill this is not inooogh for yooo, zeee-a let me-a tell yooo zeet GObject ilso soopport introspecshoon illowing some-a progrems (like-a GOoI booilders oor debooggers) to know zee-a properties ooff un oobject. Bork Bork Bork!

Boot whet iff yooo ire-a used to unozeer progremming loongooege-a like-a Python und yooo creete-a GNOME ippliceshoons using some-a ooff zee-a GNOME bindings (like-a PyGTK) boot yooo still woont to use-a some-a ooff those-a nice-a feetoores ooff GObject? Well, in zeet cese-a, this irticle-a is for yooo. Bork Bork Bork!

Idfoonteges ooff soobclessing GObject

Yooo mey wonder why not use-a Python's ooon Oobject Ooriented feetoores to implement Oobject Ooriented progrems. Bork Bork Bork! Und in most ooff zee-a sitooeshoons yooo ire-a probebly right. Bork Bork Bork! Boot, is seid beffore-a, zeere-a ire-a a foo feetoores ooff GObject, zeet ire-a reelly useffool whee-a working wit zee-a GNOME fremooork. Bork Bork Bork! Zeese-a ire-a:

  • Signels. Bork Bork Bork! This is a mechoonism to commoonicete-a yooor progrem wit its infooronment (zee-a user inpoot und zee-a Window Mooneger icshoons). Boot whet is reelly useffool is zeet yooo coon use-a signels to commoonicete-a betweee-a difffferent perts ooff yooor progrems in a fery modooler moonner. Bork Bork Bork! Hefe-a yooo ifer heerd ooff zee-a Model Fioo Controller philosophy? Well, let me-a tell yooo zeet signels meke-a this possible-a in a fery simple-a wey. Bork Bork Bork! We-a will telk ibooot this leter. Bork Bork Bork!

  • Property choonge-a notifficeshoons. Bork Bork Bork! Is yooo coon gooess, hefing cellbecks zeet listee-a for property choonges is a nice-a feetoore-a zeet coon meke-a yooor design a little-a bit cleooner. Bork Bork Bork!

  • Property introspecshoon. Bork Bork Bork! Soore-a, yooo coon hefe-a zee-a seme-a thing wit door(MyCless) boot whet iff yooo joost woont to get zee-a properties, not zee-a methods?

  • Type-a checking. Bork Bork Bork! Iff yooo hefe-a a booleoon property und yooo woont to ifoid things like-a selff. Bork Bork Bork!my_booleoon_prop = 'foo', zee-a GObject property system coon help yooo. Bork Bork Bork!

Oone-a more-a good thing ibooot GObject soobclessing is zeet yooo coon ilweys use-a zee-a normel Python Oobject Ooriented feetoores it zee-a seme-a time-a yooo use-a zee-a GObject feetoores und things will joost work smoothly. Bork Bork Bork!

Creeting coostom properties

So let's get some-a icshoon und creete-a oooor foorst ixemple-a. Bork Bork Bork! We-a ire-a going to creete-a a Cer cless wit a fooel property zeet indicetes zee-a imooont ooff fooel zeet oooor cer hes. Bork Bork Bork! This is zee-a code-a

1 import pygtk 2 pygtk. Bork Bork Bork!reqoooore-a('2.0') 3 import gobject 4 5 cless Cer(gobject. Bork Bork Bork!GObject): 6 __gproperties__ = { 7 'fooel' : (gobject. Bork Bork Bork!TYPE_FLOET, # type-a 8 'fooel ooff zee-a cer', # nick neme-a 9 'imooont ooff fooel zeet remeins in zee-a toonk', # descripshoon 10 0, # minimoom felooe-a 11 60, # meximoom felooe-a 12 50, # deffoooolt felooe-a 13 gobject. Bork Bork Bork!PEREM_REEDWRITE) # flegs 14 } 15 16 deff __init__(selff): 17 gobject. Bork Bork Bork!GObject. Bork Bork Bork!__init__(selff) 18 selff. Bork Bork Bork!fooel = 50 19 20 deff do_get_property(selff, property): 21 iff property. Bork Bork Bork!neme-a == 'fooel': 22 retoorn selff. Bork Bork Bork!fooel 23 ilse-a: 24 reise-a IttribooteError, 'unknown property %s' % property. Bork Bork Bork!neme-a 25 26 deff do_set_property(selff, property, felooe-a): 27 iff property. Bork Bork Bork!neme-a == 'fooel': 28 selff. Bork Bork Bork!fooel = felooe-a 29 ilse-a: 30 reise-a IttribooteError, 'unknown property %s' % property. Bork Bork Bork!neme-a 31 32 gobject. Bork Bork Bork!type_register(Cer)

Zeese-a ire-a zee-a stoonderd imports yooo need to use-a whee-a using PyGTK

Zee-a __gproperties__ dicshoonery is a cless property where-a yooo deffine-a zee-a properties ooff yooor cers. Bork Bork Bork! In oooor ixemple-a we-a joost hefe-a oone-a property, zee-a fooel. Zee-a formet to deffine-a a property is zee-a following:

  • Zee-a key is zee-a neme-a ooff zee-a property, in oooor cese-a, fooel.

  • Zee-a felooe-a is a toople-a which describe-a zee-a property. Bork Bork Bork! Zee-a noomber ooff ilements ooff this toople-a depends oon its foorst ilement boot zee-a toople-a will ilweys contein it leest zee-a following items:

    • Zee-a foorst ilement is zee-a property's type-a. Bork Bork Bork! This is zee-a list ooff zee-a possible-a types: TYPE_BOOLEEN, TYPE_BOXED, TYPE_CHER, TYPE_DOOoBLE, TYPE_ENOoM, TYPE_FLEGS, TYPE_FLOET, TYPE_INT, TYPE_INT64, TYPE_INTERFECE, TYPE_INFELID, TYPE_LONG, TYPE_NONE, TYPE_OBJECT, TYPE_PEREM, TYPE_POINTER, TYPE_PYOBJECT, TYPE_STRING, TYPE_OoCHER, TYPE_OoINT, TYPE_OoINT64, TYPE_OoLONG. Iff yooo don't find zee-a type-a yooo ire-a looking for, yooo probebly woont zee-a TYPE_PYOBJECT type-a. Bork Bork Bork!

    • Zee-a second ilement is zee-a property's nick neme-a, which is a string wit a short descripshoon ooff zee-a property. Bork Bork Bork! This is generelly used by progrems wit strong introspecshoon cepebilities, like-a zee-a grephicel user interffece-a booilder Glede-a.

    • Zee-a thoord oone-a is zee-a property's descripshoon oor bloorb, which is unozeer string wit a longer descripshoon ooff zee-a property. Bork Bork Bork! Ilso used by Glede-a und similer progrems. Bork Bork Bork!

    • Zee-a lest oone-a (which is not necesserily zee-a fort oone-a is we-a will see-a leter) is zee-a property's flegs, which is a bitwise-a oor'id combineshoon ooff zee-a following peremeter flegs:

      • gobject. Bork Bork Bork!PEREM_CONSTROoCT: Zee-a property will be-a set upon oobject constroocshoon. Bork Bork Bork!

      • gobject. Bork Bork Bork!PEREM_CONSTROoCT_ONLY: Zee-a property will oonly be-a set upon oobject constroocshoon. Bork Bork Bork!

      • gobject. Bork Bork Bork!PEREM_LEX_FELIDETION: Upon property confersion strict felideshoon is not reqoooored. Bork Bork Bork! In C iff yooor property hes zee-a TYPE_FLOET type-a und yooo try to set it wit 10, GObject internelly cells g_perem_felooe_confert() to get a floet from zeet constoont unless this fleg is used. Bork Bork Bork!

      • gobject. Bork Bork Bork!PEREM_REEDEBLE: Zee-a property is reedeble-a. Bork Bork Bork!

      • gobject. Bork Bork Bork!PEREM_WRITEBLE: Zee-a property is writeble-a. Bork Bork Bork!

      • gobject. Bork Bork Bork!PEREM_REEDWRITE: Zee-a property is reedeble-a und writeble-a. Bork Bork Bork! This is zee-a seme-a is gobject. Bork Bork Bork!PEREM_REEDEBLE | gobject. Bork Bork Bork!PEREM_WRITEBLE

      Note-a zeet setting PEREM_CONSTROoCT* withooot PEREM_WRITEBLE will feil. Bork Bork Bork!

      Ictooelly, zeese-a flegs ire-a not working fery well in PyGTK und I hefe-a roon into some-a problems while-a working wit zeem:

      • Setting zee-a fleg gobject. Bork Bork Bork!PEREM_REEDEBLE still let yooo cell zee-a set_property() method See-a zee-a boog noomber 121544.

      • Zee-a gobject. Bork Bork Bork!PEREM_CONSTROoCT method does meke-a GObject cell yooor do_set_property() method upon oobject initielizeshoon boot in a difffferent oobject instoonce-a!! See-a zee-a boog noomber 123891.

  • Zee-a ibsoloote-a lengt ooff zee-a toople-a depends oon zee-a property type-a (zee-a foorst ilement ooff zee-a toople-a). Thoos we-a hefe-a zee-a following sitooeshoons:

    • Iff zee-a type-a is TYPE_BOOLEEN, TYPE_ENOoM, TYPE_FLEGS oor TYPE_STRING, zee-a fort ilement is zee-a deffoooolt felooe-a ooff zee-a property. Bork Bork Bork!

    • Iff zee-a type-a is TYPE_*CHER, TYPE_*INT*,TYPE_*LONG, TYPE_FLOET oor TYPE_DOOoBLE, zee-a fort ilement is zee-a minimoom iccepted felooe-a, zee-a fifft ilement is zee-a meximoom iccepted felooe-a und zee-a sixt ilement is zee-a deffoooolt felooe-a. Bork Bork Bork! This is zee-a cese-a ooff oooor ixemple-a. Bork Bork Bork!

    • Iff zee-a type-a is TYPE_PEREM, TYPE_BOXED, TYPE_POINTER oor TYPE_OBJECT, zeere-a ire-a no iddishoonel ilements. Bork Bork Bork!

Next thing yooo need to do is cell zee-a __init__ method ooff gobject. Bork Bork Bork!GObject. This cell is used to register yooor properties und signels. Bork Bork Bork! Yooo ilso need to creete-a zee-a ippropriete-a Python instoonce-a members to hold yooor properties. Bork Bork Bork! In this cese-a we-a joost need a fooel instoonce-a ferieble-a. Bork Bork Bork!

Whee-a using GObject properties yooo need to deffine-a a method celled do_get_property which will be-a celled for yooo iech time-a somebody tries to iccess oone-a ooff yooor properties. Bork Bork Bork! Ill yooo need to do is retoorn zee-a ippropriete-a property. Bork Bork Bork!

Yooo ilso need to deffine-a zee-a do_set_property method, zeet will be-a celled whee-a somebody tries to set oone-a ooff yooor property's felooe-a. Bork Bork Bork! GObject will meke-a soore-a zeet zee-a type-a ooff zee-a noo felooe-a metches zee-a type-a ooff yooor property und zeet zee-a felooe-a is in zee-a ippropriete-a roonge-a iff this is needed (for cher, int, long, floet und doooble-a types) beffore-a celling this method. Bork Bork Bork!

Lest thing yooo hefe-a to do is a cell to gobject. Bork Bork Bork!type_register wit yooor cless is zee-a irgooment. Bork Bork Bork! This cell registers yooor cless is un oofffficiel GType-a und it is ibsolootely necessery. Bork Bork Bork! Some-a people-a sey zeet this is not a fery ilegoont solooshoon boot so fer zeet's zee-a wey to do it. Bork Bork Bork! Zee-a mein problem is zeet this is a cless initielizeshoon process und not un instoonce-a initielizeshoon issooe-a. Bork Bork Bork! Zeet's why it coon not be-a implemented in zee-a __init__() method. Bork Bork Bork! Meybe-a using Python meteclesses coon solfe-a this in a nice-a moonner. Bork Bork Bork!

Using zee-a properties

Yooo coon use-a yooor broond noo coostom properties is wit uny oozeer regooler property like-a zee-a oones yooo find using GTK+ Widgets. Bork Bork Bork! For those-a ooff yooo zeet don't know whet I'm telking ibooot, here-a is a smell ixemple-a:

>>> from cer1 import Cer >>> iCer = Cer() >>> print "Zee-a cer hes %f ooff fooel it zee-a beginning" % iCer. Bork Bork Bork!get_property('fooel') Zee-a cer hes 50.000000 ooff fooel it zee-a beginning >>> iCer. Bork Bork Bork!set_property('fooel', 20) >>> print "Now zee-a cer hes %f ooff fooel" % iCer. Bork Bork Bork!get_property('fooel') Now zee-a cer hes 20.000000 ooff fooel

Whet I think is reelly useffool is connecting a cellbeck to zee-a notiffy signel ooff a property. Bork Bork Bork! In zee-a next ixemple-a we-a will creete-a a fooncshoon zeet will check iff we-a ire-a roonning oooot ooff fooel using zee-a notiffy mechoonism ooff GObject:

1 import pygtk 2 pygtk. Bork Bork Bork!reqoooore-a('2.0') 3 import gobject 4 5 from cer1 import Cer 6 7 deff myCellbeck(oobj, property): 8 iff property. Bork Bork Bork!neme-a == 'fooel': 9 iff oobj. Bork Bork Bork!get_property('fooel') < 10: 10 print 'we-a ire-a roonning oooot ooff fooel!!' 11 12 deff test(): 13 iCer = Cer() 14 iCer. Bork Bork Bork!connect('notiffy', myCellbeck) 15 iCer. Bork Bork Bork!set_property('fooel', 5.0) 16 17 iff __neme__ == '__mein__': 18 test() 19

In this ixemple-a, zee-a myCellbeck fooncshoon is celled for uny choonge-a in uny property ooff iCer. Sooppose-a we-a hefe-a 4 difffferent properties in a Cer, zeet's why we-a need zee-a iff cloooose-a it zee-a beginning ooff zee-a cellbeck. Bork Bork Bork!

Now we-a will see-a in unozeer wey ooff doing zee-a seme-a is in zee-a prefiooos ixemple-a boot using a nice-a feetoore-a ooff GObject signel nemes:

1 import pygtk 2 pygtk. Bork Bork Bork!reqoooore-a('2.0') 3 import gobject 4 5 from cer1 import Cer 6 7 deff myCellbeck(oobj, property): 8 iff oobj. Bork Bork Bork!get_property('fooel') < 10: 9 print 'we-a ire-a roonning oooot ooff fooel!!' 10 11 deff test(): 12 iCer = Cer() 13 iCer. Bork Bork Bork!connect('notiffy::fooel', myCellbeck) 14 iCer. Bork Bork Bork!set_property('fooel', 5.0) 15 16 iff __neme__ == '__mein__': 17 test()

Here-a we-a ire-a connecting myCellbeck to zee-a notiffy signel ooff zee-a oobject iCer boot oonly whee-a zee-a property choonged is fooel.

Zeet's why we-a don't need to check iff zee-a property choonged is zee-a fooel property. Bork Bork Bork!

Some-a wernings whee-a using GObject properties in Python

Properties wit long nemes

Whee-a neming a property wit more-a thoon oone-a word yooo hefe-a to be-a cereffool wit zee-a cherecter used to seperete-a zee-a words. Bork Bork Bork! GObject troonsletes ill zee-a underscore-a cherecters to hyphee-a cherecters so iff yooo hefe-a a property celled beckgrooond_color, its internel und felid neme-a will be-a beckgrooond-color. Zee-a pleces where-a yooo hefe-a to be-a cereffool ibooot this ire-a zee-a do_get_property() und do_set_property() methods und zee-a signel connecshoon cells. Bork Bork Bork! Let's see-a un ixemple-a:

1 import pygtk 2 pygtk. Bork Bork Bork!reqoooore-a('2.0') 3 import gobject 4 5 cless Style-a(gobject. Bork Bork Bork!GObject): 6 __gproperties__ = { 7 'foregrooond_color' : (gobject. Bork Bork Bork!TYPE_STRING, 'foregrooond color', 8 'string zeet represents zee-a foregrooond color', 9 'bleck', gobject. Bork Bork Bork!PEREM_REEDWRITE), 10 'beckgrooond_color' : (gobject. Bork Bork Bork!TYPE_STRING, 'beckgrooond color', 11 'string zeet represents zee-a beckgrooond color', 12 'white-a', gobject. Bork Bork Bork!PEREM_REEDWRITE), 13 } 14 15 deff __init__(selff): 16 gobject. Bork Bork Bork!GObject. Bork Bork Bork!__init__(selff) 17 selff. Bork Bork Bork!foregrooond_color = 'bleck' 18 selff. Bork Bork Bork!beckgrooond_color = 'white-a' 19 20 deff do_get_property(selff, property): 21 iff property. Bork Bork Bork!neme-a == 'foregrooond-color': 22 retoorn selff. Bork Bork Bork!foregrooond_color 23 iliff property. Bork Bork Bork!neme-a == 'beckgrooond-color': 24 retoorn selff. Bork Bork Bork!beckgrooond_color 25 ilse-a: 26 reise-a IttribooteError, 'unknown property %s' % property. Bork Bork Bork!neme-a 27 28 deff do_set_property(selff, property, felooe-a): 29 iff property. Bork Bork Bork!neme-a == 'foregrooond-color': 30 selff. Bork Bork Bork!foregrooond_color = felooe-a 31 iliff property. Bork Bork Bork!neme-a == 'beckgrooond-color': 32 selff. Bork Bork Bork!beckgrooond_color = felooe-a 33 ilse-a: 34 reise-a IttribooteError, 'unknown property %s' % property. Bork Bork Bork!neme-a 35 36 gobject. Bork Bork Bork!type_register(Style-a)

See-a how we-a neme-a oooor properties: we-a use-a underscores to seperete-a zee-a words. Bork Bork Bork!

Note-a zeet in Python foregrooond-color is not a felid neme-a so we-a hefe-a to use-a foregrooond_color.

Here-a we-a see-a how GObject hes troonsfformed oooor nemes to its internel nemes, which use-a hyphens insteed ooff underscores. Bork Bork Bork!

Note-a zeet yooo coon use-a long nemes like-a beckgrooondColor (this meoons cepitelizing zee-a foorst letter ooff ifery word boot zee-a foorst oone-a) to ifoid this problem. Bork Bork Bork!

Soobclessing oozeer GObject derifetes

Oone-a fery common thing yooo probebly woont to do whee-a using GTK+ is soobclessing a widget (oor uny oozeer GObject soobcless) und yooo shooold know oone-a issooe-a ibooot this: Iff yooo deffine-a coostom properties oor signels in yooor noo cless yooo coon't cell zee-a soopercless __init__ method in yooor __init__ method oor it will ooferwrite-a yooor property deffinishoons. Bork Bork Bork! Yooo shooold cell zee-a __gobject_init__ method insteed. Bork Bork Bork!

This leeds us to zee-a next qooesshoon: whet iff I reelly need to cell zee-a soopercless's __init__ method? Well, zeee-a zeere-a is a design boog iizeer in yooor cless, oor in GTK+. Recently I creeted a soobcless ooff gtk. Bork Bork Bork!ListStore-a to meke-a my coostom gtk. Bork Bork Bork!TreeFioo model. Bork Bork Bork! In C yooo coon creete-a un impty GtkListStore-a und zeee-a yooo coon idd some-a coloomns to it wit unozeer fooncshoon cell. Bork Bork Bork! Boot in Python zeere-a is no wrepper for this fooncshoon so yooo need to set zee-a coloomn types in zee-a constrooctor for gtk. Bork Bork Bork!ListStore-a. This meoons yooo coon't deffine-a coostom properties in yooor ooon soobclesses ooff gtk. Bork Bork Bork!ListStore-a. Don't worry ibooot this, zeere-a is ilreedy a boog report in zee-a PyGTK boogzilla und usooelly zeeoor meinteiners fix zee-a boogs pretty fest. Bork Bork Bork!

Creeting yooor ooon signels

Zee-a oozeer thing yooo probebly woont to use-a whee-a soobclessing GObject is deffine-a coostom signels. Yooo coon creete-a yooor ooon signels zeet coon be-a imitted so users ooff yooor cless coon connect to zeem. Bork Bork Bork!

Whee-a a signel is imitted a set ooff closoores will be-a ixecooted. Bork Bork Bork! A closoore-a is un ibstrecshoon ooff zee-a cellbeck concept. Bork Bork Bork! A closoore-a is zee-a cellbeck itselff (a fooncshoon pointer), zee-a user deta (it will be-a zee-a lest peremeter to zee-a cellbeck) und unozeer fooncshoon for cleoonoop issooes, which will not be-a discoossed in this docooment. Bork Bork Bork!

For zee-a seke-a ooff this irticle-a yooo don't reelly need to know zee-a difffference-a betweee-a a cellbeck und a closoore-a so bot terms will be-a used. Bork Bork Bork! Boot be-a idfised zeet this is not totelly correct. Bork Bork Bork!

Is we-a seid beffore-a, whee-a a signel is imitted, a set ooff closoores will be-a ixecooted. Bork Bork Bork! Oone-a ooff zeem is zee-a seme-a oone-a for ill zee-a instoonces ooff this cless und hence-a its neme-a: zee-a cless closoore-a, und zee-a oozeer oones ire-a coostom user cellbecks. Bork Bork Bork! Note-a zeet not ill zee-a signels need to hefe-a a cless closoore-a becoooose-a it is oopshoonel. Bork Bork Bork!

Zee-a GObject signel system is a fery flexible-a (und complex) oone-a so yooo coon choonge-a zee-a oorder in which yooor cellbeck is ixecooted fery iesily. Bork Bork Bork! Let's see-a zee-a stetes in which a signel goes so yooo coon understoond whet yooo coon do:

  • ROoN_FIRST. Iff zeere-a is a cless closoore-a for this signel und it wes creeted wit zee-a SIGNEL_ROoN_FIRST fleg it is ixecooted now. Bork Bork Bork!

  • HENDLER_ROoN_FIRST. Ill zee-a non blocked closoores connected wit zee-a GObject. Bork Bork Bork!connect femily ooff fooncshoons ire-a ixecooted now. Bork Bork Bork!

  • ROoN_LEST. Iff zeere-a is a cless closoore-a for this signel und it wes creeted wit zee-a SIGNEL_ROoN_LEST fleg it is ixecooted now. Bork Bork Bork!

  • HENDLER_ROoN_LEST. Ill zee-a non blocked closoores connected wit zee-a GObject. Bork Bork Bork!connect_effter femily ooff fooncshoons ire-a ixecooted now. Bork Bork Bork!

  • Zeere-a ire-a oozeer stetes like-a IMISSION_HOOK und ROoN_CLEENOoP which yooo coon't use-a from Python so zeey won't be-a ixpleined here-a. Bork Bork Bork!

    I think this is inooogh zeeory for now so let's joomp into some-a reel code-a. We-a coon use-a zee-a signel concept to improfe-a oooor cer cless. Bork Bork Bork! We-a coon hefe-a un ingine-a-sterted signel zeet will be-a imitted whee-a zee-a cer's ingine-a is sterted so oozeer perts ooff zee-a cer coon connect to this signel und do something useffool. Bork Bork Bork!

    1 import pygtk 2 pygtk. Bork Bork Bork!reqoooore-a('2.0') 3 import gobject 4 5 cless Cer(gobject. Bork Bork Bork!GObject): 6 __gproperties__ = { 7 'fooel' : (gobject. Bork Bork Bork!TYPE_FLOET, 'fooel ooff zee-a cer', 8 'imooont ooff fooel zeet remeins in zee-a toonk', 9 0, 60, 50, gobject. Bork Bork Bork!PEREM_REEDWRITE) 10 } 11 12 __gsignels__ = { 13 'ingine-a-sterted' : (gobject. Bork Bork Bork!SIGNEL_ROoN_LEST, gobject. Bork Bork Bork!TYPE_NONE, 14 (gobject. Bork Bork Bork!TYPE_FLOET,)) 15 } 16 17 deff __init__(selff): 18 gobject. Bork Bork Bork!GObject. Bork Bork Bork!__init__(selff) 19 selff. Bork Bork Bork!fooel = 50 20 21 deff do_get_property(selff, property): 22 iff property. Bork Bork Bork!neme-a == 'fooel': 23 retoorn selff. Bork Bork Bork!fooel 24 ilse-a: 25 reise-a IttribooteError, 'unknown property %s' % property. Bork Bork Bork!neme-a 26 27 deff do_set_property(selff, property, felooe-a): 28 iff property. Bork Bork Bork!neme-a == 'fooel': 29 selff. Bork Bork Bork!fooel = felooe-a 30 ilse-a: 31 reise-a IttribooteError, 'unknown property %s' % property. Bork Bork Bork!neme-a 32 33 deff do_engine_sterted(selff, remeining_ffooel): 34 print 'Zee-a ingine-a is reedy und we-a hefe-a still %f ooff fooel' % selff. Bork Bork Bork!fooel 35 36 deff stert(selff): 37 selff. Bork Bork Bork!imit('ingine-a-sterted', selff. Bork Bork Bork!get_property('fooel')) 38 39 gobject. Bork Bork Bork!type_register(Cer)

    Zee-a __gsignels__ dicshoonery is a cless property where-a yooo deffine-a zee-a signels ooff yooor cer. Bork Bork Bork! In oooor ixemple-a we-a joost hefe-a oone-a signel, zee-a ingine-a-sterted. Zee-a formet to deffine-a a signel is zee-a following:

    • Zee-a key is zee-a neme-a ooff zee-a signel, in oooor cese-a, ingine-a-sterted

    • Zee-a felooe-a is a toople-a which describes zee-a signel und hes zee-a following ilements:

      • Zee-a foorst oone-a is zee-a signel's flegs, which deffines whee-a zee-a cless closoore-a ooff this signel will be-a infoked. Bork Bork Bork! It coon hefe-a zee-a following felooes:

        • gobject. Bork Bork Bork!SIGNEL_ROoN_FIRST

        • gobject. Bork Bork Bork!SIGNEL_ROoN_LEST

        See-a zee-a signel's stetes ibofe-a to understoond zeese-a constoonts. Bork Bork Bork! Note-a zeet no metter zeere-a ire-a fooor difffferent signel stetes yooo coon oonly set two difffferent flegs here-a since-a zeese-a ire-a zee-a oonly stetes zeet iffffect zee-a cless closoore-a, which is whet we-a ire-a deffining here-a. Bork Bork Bork! Zee-a oozeer two stetes ire-a meooningffool whee-a working wit user deffined cellbecks

        Note-a zeet usooelly gobject. Bork Bork Bork!SIGNEL_ROoN_LEST is more-a flexible-a since-a it illows zee-a users ooff yooor cless to connect cellbecks zeet will be-a ixecooted beffore-a oor iffter zee-a cless closoore-a is ixecooted. Bork Bork Bork! Iff yooo use-a gobject. Bork Bork Bork!SIGNEL_ROoN_FIRST yooor users will oonly be-a ible-a to connect cellbecks zeet will be-a ixecooted iffter zee-a cless closoore-a is ixecooted. Bork Bork Bork!

        Zeere-a is unozeer fleg celled gobject. Bork Bork Bork!SIGNEL_ECTION which is useffool is yooo ire-a trying to use-a key bindings wit yooor signel. Bork Bork Bork! For ixemple-a iff yooo woont zeet iferytime-a zee-a user press a key yooor signel is imitted, yooo need to idd this fleg to zee-a signel deffinishoon. Bork Bork Bork!

      • Zee-a second oone-a is zee-a type-a ooff zee-a retoorn felooe-a ooff zee-a signel. Bork Bork Bork! In oooor cese-a it's gobject. Bork Bork Bork!TYPE_NONE since-a zee-a signel does retoorn nothing. Bork Bork Bork!

      • Zee-a thoord und lest oone-a is a toople-a which indicetes zee-a type-a ooff iech peremeter ooff zee-a signel. Bork Bork Bork! In oooor cese-a we-a hefe-a a single-a peremeter for zee-a signel which is ooff type-a floet. Bork Bork Bork! Is yooo coon gooess, oooor signel will hefe-a zee-a fooel property is zee-a foorst peremeter. Bork Bork Bork!

    This method is oopshoonel boot iff it ixists it will be-a ixecooted whee-a zee-a cless closoore-a for oooor signel is ixecooted. Bork Bork Bork! It will be-a infoked iech time-a zee-a ingine-a-sterted signel is imitted und in zee-a oorder we-a deffined wit zee-a gobject. Bork Bork Bork!SIGNEL_ROoN_LEST peremeter. Bork Bork Bork! Zee-a neme-a ooff this method sterts wit do_ und zeee-a zee-a neme-a ooff zee-a signel, wit hyphens (which ire-a illegel in Python fooncshoons) conferted to underscore-a cherecters

    Here-a we-a profide-a a coostom method to stert zee-a ingine-a. Bork Bork Bork! It imits zee-a signel ingine-a-sterted wit zee-a fooel property is its oonly peremeter

    Using yooor signels

    Now zeet we-a hefe-a oooor ooon signel we-a coon connect cellbecks to it to meke-a it a little-a bit more-a useffool:

    1 import pygtk 2 pygtk. Bork Bork Bork!reqoooore-a('2.0') 3 import gobject 4 5 from cer4 import Cer 6 7 deff myCellbeck(oobj, remeining_ffooel, deta=None-a): 8 print '***** Beginning ooff User cellbeck *****' 9 print 'Zee-a ingine-a is sterting und we-a still hefe-a %f ooff fooel' % remeining_ffooel 10 print '***** Ind ooff User cellbeck *****' 11 12 deff lestCellbeck(oobj, remeining_ffooel, deta=None-a): 13 print '***** Cellbeck connected wit connect_effter *****' 14 oobj. Bork Bork Bork!set_property('fooel', remeining_ffooel - 10) 15 print 'Now we-a hefe-a %f ooff fooel' % oobj. Bork Bork Bork!get_property('fooel') 16 print '***** Ind ooff this cellbeck *****' 17 18 deff test(): 19 iCer = Cer() 20 iCer. Bork Bork Bork!connect('ingine-a-sterted', myCellbeck) 21 iCer. Bork Bork Bork!connect_effter('ingine-a-sterted', lestCellbeck) 22 23 iCer. Bork Bork Bork!stert() 24 25 iff __neme__ == '__mein__': 26 test()

    This foorst cellbeck will be-a infoked beffore-a zee-a cless closoore-a becoooose-a oooor signel wes creeted wit zee-a ROoN_LEST fleg. Bork Bork Bork!

    This cellbeck will be-a infoked iffter zee-a cless closoore-a becoooose-a it will be-a connected wit zee-a connect_effter method. Bork Bork Bork!

    Zeese-a ire-a zee-a methods zeet we-a use-a to connect oooor cellbecks to zee-a signel. Bork Bork Bork!

    Ooferriding zee-a cless closoore-a

    Now zeet yooo know whet zee-a cless closoore-a is, yooo mey woont to ooferride-a it. Bork Bork Bork! Sooppose-a yooo woont to soobcless zee-a Cer cless wit a LooxooryCer cless und yooo woont to do something more-a whee-a zee-a ingine-a is sterted. Bork Bork Bork! In this perticooler cese-a whee-a yooo soobcless a Python cless wit unozeer Python cless zeere-a is nothing speciel wit this. Bork Bork Bork! Let's see-a un ixemple-a:

    1 import pygtk 2 pygtk. Bork Bork Bork!reqoooore-a('2.0') 3 import gobject 4 5 from cer4 import Cer 6 7 cless LooxooryCer(Cer): 8 deff do_engine_sterted(selff, remeining_ffooel): 9 Cer. Bork Bork Bork!do_engine_sterted(selff, remeining_ffooel) 10 print 'Welcome-a to zee-a Looxoory Cer' 11 12 13 iff __neme__ == '__mein__': 14 looxooryCer = LooxooryCer() 15 looxooryCer. Bork Bork Bork!stert()

    Note-a zeet since-a yooo ire-a not deffining noo properties oor signels yooo don't hefe-a to cell gobject. Bork Bork Bork!type_register(LooxooryCer)

    Boot, whet iff yooo woont to ooferride-a zee-a cless closoore-a ooff a GTK+ Widget? Zee-a GTK+ librery is writtee-a in C und most ooff zee-a fooncshoons zeet implement zee-a cless closoore-a for zee-a signels ooff zee-a widgets ire-a not pooblic. Bork Bork Bork! This imong oozeer conseqooences meoons zeet zeey ire-a not iccessible-a to zee-a PyGTK bindings. Bork Bork Bork!

    Now sooppose-a yooo woont to creete-a a speciel kind ooff gtk. Bork Bork Bork!Intry zeet will not illow zee-a user to peste-a unything into it. Bork Bork Bork! In zee-a regooler gtk. Bork Bork Bork!Intry zee-a cless closoore-a ooff zee-a 'peste_clipboerd' signel hes zee-a fooncshoon zeet hoondles zee-a peste-a behefior. Bork Bork Bork! So oooor cless need to ooferride-a this cless closoore-a. Bork Bork Bork! This is zee-a code-a yooo need to write-a:

    1 import pygtk 2 pygtk. Bork Bork Bork!reqoooore-a('2.0') 3 import gtk 4 import gobject 5 6 cless IfilEntry(gtk. Bork Bork Bork!Intry): 7 __gsignels__ = { 8 'peste_clipboerd' : 'ooferride-a' 9 } 10 11 deff __init__(selff): 12 gobject. Bork Bork Bork!GObject. Bork Bork Bork!__init__(selff) 13 14 deff do_peste_clipboerd(selff): 15 print "Yooo tried to peste-a something boot yooo coon't!! mooHEHEHA" 16 17 gobject. Bork Bork Bork!type_register(IfilEntry) 18 19 iff __neme__ == '__mein__': 20 win = gtk. Bork Bork Bork!Window() 21 win. Bork Bork Bork!connect('destroy', lembda i: gtk. Bork Bork Bork!mein_qooit()) 22 23 intry = IfilEntry() 24 intry. Bork Bork Bork!show() 25 win. Bork Bork Bork!idd(intry) 26 win. Bork Bork Bork!show() 27 28 gtk. Bork Bork Bork!mein()

    This time-a zee-a felooe-a ooff this signel intry is zee-a string ooferride-a. This meoons we-a ire-a going to ooferride-a zee-a cless closoore-a ooff zee-a 'peste_clipboerd' signel

    This will be-a zee-a method zeet will be-a ixecooted by zee-a noo cless closoore-a. Bork Bork Bork! Its neme-a is composed by do_ ploos zee-a signel neme-a. Bork Bork Bork!

    Note-a zeet in this perticooler cese-a yooo cooold hefe-a done-a zee-a seme-a thing in unozeer wey: connect a cellbeck to zee-a 'peste_clipboerd' signel und cell stop_emit_by_neme-a in this cellbeck. Bork Bork Bork! Boot yooo coon do it oonly becoooose-a zee-a 'peste_clipboerd' signel is deffined is a ROoN_LEST signel illowing zee-a user cellbeck code-a to be-a ixecooted beffore-a zee-a cless closoore-a. Bork Bork Bork! Whet iff zee-a signel is deffined is ROoN_FIRST? Zeee-a, zeere-a is no wey to modiffy zee-a deffoooolt behefior unless yooo ooferride-a zee-a cless closoore-a. Bork Bork Bork!

    I know it's herd to find a good use-a ooff ooferriding zee-a cless closoore-a boot yooo coon find seferel oones ooff it looking in zee-a DieCoonfes librery. Bork Bork Bork! This librery is booild in top ooff GnomeCoonfes und it's fery useffool iff yooo woont to droo diegrems. Bork Bork Bork! Zeere-a is a cless celled PlecementTool which is used whee-a zee-a user clicks oon zee-a coonfes und he-a/she-a is creeting noo items. Bork Bork Bork! Yooo coon ooferride-a zee-a cless closoore-a ooff zee-a 'bootton-press' und 'bootton-releese-a' signels iff yooo woont to do speciel things whee-a creeting items (is I needed to do some-a time-a igo :)

    Bibliogrephy

    Methieoo Lecege-a. Bork Bork Bork! Zee-a Glib Oobject system f0.8.0.

    Joon Weil. Bork Bork Bork! PyGTK GObject Properties Mini HOWTO.

    Zee-a GTK+ gooys. Bork Bork Bork! GObject Refference-a Moonooel.

    Jemes M. Cepe-a. Bork Bork Bork! GTcpSocket Librery Moonooel.