5

Is there a way I can tell gtk to automatically call gtk.main_quit() when the last open window of the application is closed/destroyed?

If there is no direct feature offering this functionality, I could think of the following: In the window's destroy method: get a list of open windows in the process, if its empty quit. Is there a way to get such a list?

The obvious solution would be to keep manually track of all open windows, but I would want to avoid this if possible.

rumpel
  • 7,870
  • 2
  • 38
  • 39
  • 1
    Have you tried looked at [PyGTK reference (window-get-toplevels)](http://library.gnome.org/devel/pygtk/stable/class-gdkwindow.html#function-gdk--window-get-toplevels) – spicavigo Sep 27 '11 at 10:38

2 Answers2

6

the destroy signal of the main window must be connected to gtk main_quit :

window.connect("destroy", gtk.main_quit)
Louis
  • 2,854
  • 2
  • 19
  • 24
  • 1
    I think OP means a situation where there are multiple main windows. Think of a word editor that opens a new window for each active document. – patrys Sep 27 '11 at 12:21
  • If this is the case, one may just count the opened windows, adding one to a global variable when created and subtracting one when destroyed should be sufficient, no need to keep the list. – Louis Sep 27 '11 at 12:46
  • 2
    You could also use GtkAppliction if you're application fits the logic. That will be better and GtkApplication will handle that logic for you. – erick2red Sep 28 '11 at 14:13
  • @erick2red is there a pygtk equivalent for this class? – rumpel Sep 28 '11 at 15:11
  • It depends on the bindings you're using, but yes, pygobject bindings are built using GObject Introspection, so they have every functionality present on the C library. Not quite extacly what you wanted, but look at [this] (http://readthedocs.org/docs/python-gtk-3-tutorial/en/latest/). This way you can use all the GLib/Gtk+ system from python. – erick2red Sep 29 '11 at 12:36
2

Use the method gtk.main_level() to get the current nesting level of the main loop. The nesting level is increased by calling the gtk.main() function and reduced by calling the gtk.main_quit() function

athanasis
  • 29
  • 1