Python and Gtk

Mitch Chapman chapman at bioreason.com
Mon Aug 6 12:17:43 EDT 2001


Djoumé wrote:
> 
> I begin with Python and Gtk and I have some unresolved question :
> 
> I use Gtkinter and I have notice that a lot of gtk (or gdk) object miss in
> the gtk.py module. How can I do when I need these objects (for example
> Gdkfont)?

Gtkinter is included with pygtk but is deprecated.  It's
best just to use gtk.py.

The source code for pygtk includes a module called description.py.
This module exists for documentation purposes only.  It describes
classes such as GdkFont which don't have wrappers in gtk.py, but
which are used in pygtk.  (These classes don't have gtk.py wrappers,
but various methods in gtk.py may return instances of these
classes.  It's confusing...)

> Where can I find the prototype (type of arguments) of a callback function
> must have (for example where can I find the type of arguments of a button
> pressed callback funtion)?

One way to discover the interface of a callback function is
to define a dummy callback which has a "wildcard" interface.  
For example:

def buttonClickCB(*args):
    print "Button was clicked."
    print "  Arguments:", args
...
btn = gtk.GtkButton(label="Click me")
btn.connect("clicked", buttonClickCB)

When you run the program and click the button, it will print
out the argument list.

In fact, most of my callbacks never need the callback 
arguments.  (They are defined as methods on Controller classes,
and the Controllers keep all of the state needed in order
to process the callbacks.)  So I just declare callback
methods using the wildcard signature and never worry
about the "real" signature.  

Just to be redundant, here's another example:

class Controller:
    def __init__(self):
        ...
        self.fooBtn = gtk.GtkButton(label="Foo")
        self.fooBtn.connect("clicked", self.fooClickedCB)
        ...
    def fooClickedCB(self, *args):
        ...


Hope this helps...
-- 
Mitch Chapman
Mitch.Chapman at bioreason.com



More information about the Python-list mailing list