Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically close gtk window

Tags:

pygtk

gtk

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')
like image 304
Uyghur Lives Matter Avatar asked May 15 '11 00:05

Uyghur Lives Matter


People also ask

How do I destroy GTK windows?

To delete a Gtk. Window , call Gtk. Widget. destroy ().

What is GTK window?

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.


1 Answers

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()
like image 78
zap Avatar answered Sep 21 '22 17:09

zap