[pygtk] pygtk-libglade questions

Scott F. Johnston scott@fleetingimage.com
Mon, 14 Aug 2000 10:04:18 -0700


I find the best structure is illustrated in the second
of those tutorials, specifically, in:
  http://laguna.fmedic.unam.mx/~daniel/pygtutorial/pygtutorial/x185.html

Note that the GUI uses libglade, but generally isn't a derived class
from it.
Be careful to separate the GUI from the rest of the application,
especially if
you plan to "pickle" application data, as it is better to rebuild the
GUI
on a subsequent run than to "reload" the GUI objects.

Pay attention to the return value for event handlers.
GTK needs to know if you want other handlers to process
the event, or if you want GTK to stop handling the event upon
your return.

When combined with glade it is best if you DON'T delete
things like aboutboxes, but just to hide them. Once deleted,
you'll need to recreate them, whereas by hiding them, you can
just show() them again without trouble.
In some cases you may find it better to hand-code your widgets
rather than use glade. libglade is great for getting the bones
of an application together, but sometimes widgets need the
hands of a programmer.

Have fun! It's a great way to code.

The general format for a glade/GTK application is:

class NonGUIApplicationRoutines:
  ...


class GUI:
  def __init__(self):
    self.app = NonGUIApplicationRoutines()
    self.wtree = libglade.GladeXML('app.glade')
    self.wtree.get_widget('app').connect("destroy", mainquit) # Top
widget
    dict = {}
    # Create a dictionary from
    # the names of all the functions in this class.
    for key in dir(self.__class__):
      dict[key] = getattr(self, key)
    # You can manually add functions to dict that the
    # above misses, in case you've changed names
    # or call something outside of this class.

    # Bind signals from glade to the functions
    # in our dictionary.
    self.wtree.signal_autoconnect(dict)

  def on_button_clicked(self, *args):
    print "Button clicked with:", args
    # Call NonGUIApplicationRoutines.