If you have a sub-window in GTK and you want to close it programmatically (e.g., pressing a save button or the escape key), is there a preferred way to close the window?
E.g.,
window.destroy()
# versus
window.emit('delete-event')
To delete a Gtk. Window , call Gtk. Widget. destroy ().
GTK (formerly GIMP ToolKit and GTK+) is a free and open-source cross-platform widget toolkit for creating graphical user interfaces (GUIs). It is licensed under the terms of the GNU Lesser General Public License, allowing both free and proprietary software to use it.
Using destroy method doesn't work as expected, as the 'delete-event' callbacks are not called on the destroyed window, thus a editor, for example, won't have a chance to ask the user if the file has to be saved.
[3|zap@zap|~]python
Python 2.7.3 (default, Jul 24 2012, 10:05:38)
[GCC 4.7.0 20120507 (Red Hat 4.7.0-5)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import gtk
>>> w = gtk.Window()
>>> w.show()
>>> def cb(w,e):
... print "cb", w, e
... return True
...
>>> w.connect ('delete-event', cb)
>>> w.destroy()
In the above example invoking w.destroy() won't invoke the callback, while clicking on the "close" button will invoke it (and window won't close because callback returned True).
Thus, you have to both emit the signal and then destroy the widget, if signal handlers returned False, e.g:
if not w.emit("delete-event", gtk.gdk.Event(gtk.gdk.DELETE)):
w.destroy()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With