Strumenti Utente

Strumenti Sito


pygtk:overriding

pygtk methods overriding

Suppose that I have a gtk.Label (or any other gtk.Widget) label which has a method I don't like, for instance size_request. So I write a new method my_beautiful_size_request which behaves as I want.

However this won't work:

label.size_request = my_beautiful_size_request

; my method will just never be called, because apparently the C code which is under pygtk doesn't really care if the python object has a new method appended to it. This instead will work:

class MyLabel(gtk.Label):
    __gtype_name__ = 'MyLabel'
    do_size_request = my_beautiful_size_request

label = MyLabel()

; this works for (as far as I know) all pygtk methods: it is sufficient to add do_ in front of the name of a method. Notice, however, that the do_* method doesn't necessarily accept the same arguments as the original, neither return the same values (and also notice that as of January 2009, there is a bug which makes error messages totally misleading). For instance, in our case, while size_request is called by just giving the instance as argument, and returns a tuple, do_size_request is called with two arguments: the instance and a requisition, and instead of returning the two dimensions, it sets them as requisition.width and requisition.height

pygtk/overriding.txt · Ultima modifica: 2009/01/14 09:47 da pietro