Hello,

I have a small application with pygtk/libglade and I have the
following problem: I don't know how to exit the application after the
last window was closed.

Attached is a barebones application which illustrates my efforts so
far:

.
|-- run.py
|-- test.glade
`-- ui
    |-- __init__.py
        `-- glade.py

Note: I need glade.py because I am still using PyGtk-1.9.14 from
Debian.

Should I create some master reference counter of windows that calls
main_quit when it drops to 0? The problem then is that I cannot capture
"destroy_event". Should I decrease the counter on "delete_event" instead?

I have tried to decrease the counter in glade.Controller.__del__ but
that method does not get called. See glade_with_counter.py

Thank you,
florin

-- 

"NT is to UNIX what a doughnut is to a particle accelerator."
#!/usr/bin/env python

import pygtk
pygtk.require('2.0')

import gtk
import ui.glade

class Controller1(ui.glade.Controller):
   def __init__(self, gladeFile, title):
      ui.glade.Controller.__init__(self, gladeFile, 'window1')
      self.widgets.get_widget('window1').set_title(title)

   def on_button1_pressed(self, button):
      """ This should close the window, and if it is the last window,
      close the application. """
      self.widgets.get_widget('window1').destroy()

   def on_window1_destroy_event(self, window):
      """ This one doesn't get called. But even if it would, I don't want
      to call gtk.main_quit here. """
      print "destroying window1"

one = Controller1('test.glade', 'one')
two = Controller1('test.glade', 'two')
gtk.main()
<?xml version="1.0" standalone="no"?> <!--*- mode: xml -*-->
<!DOCTYPE glade-interface SYSTEM "http://glade.gnome.org/glade-2.0.dtd";>

<glade-interface>

<widget class="GtkWindow" id="window1">
  <property name="visible">True</property>
  <property name="title" translatable="yes">window1</property>
  <property name="type">GTK_WINDOW_TOPLEVEL</property>
  <property name="window_position">GTK_WIN_POS_NONE</property>
  <property name="modal">False</property>
  <property name="resizable">True</property>
  <property name="destroy_with_parent">False</property>

  <child>
    <widget class="GtkButton" id="button1">
      <property name="visible">True</property>
      <property name="can_focus">True</property>
      <property name="label" translatable="yes">button1</property>
      <property name="use_underline">True</property>
      <property name="relief">GTK_RELIEF_NORMAL</property>
      <signal name="pressed" handler="on_button1_pressed" last_modification_time="Sun, 
16 Feb 2003 16:11:25 GMT"/>
    </widget>
  </child>
</widget>

</glade-interface>
import gtk
import gobject
import gtk.glade
import new
import types
import re

class Controller:
   #This method loads the XML file and autoconnects the signals
   def __init__(self, gladeFile, windowName):

      # save to be used later when instantiating child windows
      self.gladeFile = gladeFile

      # construct the window
      self.widgets = gtk.glade.XML(gladeFile, windowName)

      callbacks = {}

      # look at methods named "on.*"
      reON = re.compile("^on")

      classesInspected = {}
      classesToInspect = [self.__class__]

      # find and store methods as bound callbacks
      while classesToInspect:
         currentClass = classesToInspect.pop()
         if not classesInspected.has_key(currentClass):
            classesInspected[currentClass] = 1
            for cc in currentClass.__bases__:
               classesToInspect.append(cc)
            classMethods = currentClass.__dict__
            for method_name in classMethods.keys():
               if reON.match(method_name) and not callbacks.has_key(method_name):
                  method = classMethods[method_name]
                  if type(method) == types.FunctionType:
                     callbacks[method_name] = new.instancemethod(method, 
                                                                 self,
                                                                 currentClass)

      # autoconnect
      self.widgets.signal_autoconnect(callbacks)
import gtk
import gobject
import gtk.glade
import new
import types
import re

counter = 0

class Controller:
   #This method loads the XML file and autoconnects the signals
   def __init__(self, gladeFile, windowName):

      global counter
      counter += 1

      # save to be used later when instantiating child windows
      self.gladeFile = gladeFile

      # construct the window
      self.widgets = gtk.glade.XML(gladeFile, windowName)

      callbacks = {}

      # look at methods named "on.*"
      reON = re.compile("^on")

      classesInspected = {}
      classesToInspect = [self.__class__]

      # find and store methods as bound callbacks
      while classesToInspect:
         currentClass = classesToInspect.pop()
         if not classesInspected.has_key(currentClass):
            classesInspected[currentClass] = 1
            for cc in currentClass.__bases__:
               classesToInspect.append(cc)
            classMethods = currentClass.__dict__
            for method_name in classMethods.keys():
               if reON.match(method_name) and not callbacks.has_key(method_name):
                  method = classMethods[method_name]
                  if type(method) == types.FunctionType:
                     callbacks[method_name] = new.instancemethod(method, 
                                                                 self,
                                                                 currentClass)

      # autoconnect
      self.widgets.signal_autoconnect(callbacks)

   def __del__(self):
      print "Controller.__del__ called"
      global counter
      counter -= 1
      if counter == 0:
         gtk.main_quit()

Attachment: msg05468/pgp00000.pgp
Description: PGP signature

Reply via email to