8

I have gtk.Window and I need to catch closure. I need to close the show the message dialog and click Yes if the window should be closed unless there is a show window

Thank you.

0xAX
  • 20,957
  • 26
  • 117
  • 206

2 Answers2

5

Handle the delete-event signal. Return False to close, True to cancel.

Johannes Sasongko
  • 4,178
  • 23
  • 34
4

Here is how I use it:

# in constructor:
        self.connect('destroy', gtk.main_quit)
        self.connect('delete-event', self.on_destroy)

    def on_destroy(self, widget=None, *data):
        # return True --> no, don't close

        messagedialog = gtk.MessageDialog(parent=self, flags= gtk.DIALOG_MODAL & gtk.DIALOG_DESTROY_WITH_PARENT, 
                                          type=gtk.MESSAGE_QUESTION, buttons=gtk.BUTTONS_OK_CANCEL, message_format="Click on 'Cancel' to leave the application open.")
        messagedialog.show_all()
        result=messagedialog.run()
        messagedialog.destroy()
        if result==gtk.RESPONSE_CANCEL:
            return True
        return False
guettli
  • 25,042
  • 81
  • 346
  • 663
  • i you use the Gtk.builder and dont subclass window you will need to do something like window = self.builder.get_object('pyWindow') window.connect('destroy', Gtk.main_quit) – semisided1 Jan 27 '15 at 16:43