Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have almost finished my Ubuntu App Showdown app, but would like to make it somewhat more robust, upon application quit I iterate through the open files checking for unsaved files, if any are found I pop up a dialog informing the user.

What I would like to happen is that if the user cancels the dialog the program resumes, if however the user clicks ok, both dialog and main window should close.

This is what I have so far.

self.connect("delete-event", self.CheckSave)

def CheckSave(self, arg1, arg2):
    unsaved = False
    for doc in self.Documents:
        if doc.Saved == False:
            unsaved = True

    if unsaved:
        print("Unsaved document")
        Dialog = Gtk.Dialog("Really quit?", 0, Gtk.DialogFlags.MODAL)
        Dialog.add_button(Gtk.STOCK_NO, Gtk.ResponseType.CANCEL)
        Dialog.add_button(Gtk.STOCK_YES, Gtk.ResponseType.OK)

        box = Dialog.get_content_area()
        label1 = Gtk.Label("There are unsaved file(s).")
        label2 = Gtk.Label("Are you sure you want to quit?")

        response =

        if response == Gtk.ResponseType.OK:


When the dialog runs it never output's the ResponseType.OK or ResponseType.CANCEL values, I get random negative numbers such as -4 or -6, the dialog also never closes and the main window keeps emitting dialog and requires a CTRL+c to quit it.

share|improve this question
up vote 3 down vote accepted

There are several issues with this code.

  • The dialog.destroy() method will never be called, you return your function before this call.

  • Have a look at the Gtk.MessageDialog. It will handle some boilerplate code which you need with a regular Gtk.Dialog.

  • Read PEP-8. It's not a rule, but common practice. UpperCase names are intended for classes, attributes and methods should be camelCase or with_underscore.

  • The for-loop is inefficient. And the whole method could be indented one tab less.

Here is some sample code, pep-8 and the messagedialog still needs to be done.

def confirm_close(self, widget, event):
    unsaved = False
    for doc in self.Documents:
        if not doc.Saved:
            unsaved = True
            break # Break here, no need to check more documents

    if not unsaved:

    #TODO: build the messagedialog
    dialog = Gtk.MessageDialog(....)
    response =
    if response == Gtk.ResponseType.OK:
        # Really quit the application
    # Close the dialog and keep the main window open
    return True
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.