Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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?")
        box.add(label1)
        box.add(label2)
        box.show_all()

        response = Dialog.run()
        print(response)

        if response == Gtk.ResponseType.OK:
            return(False)
        else:
            return(True)

        Dialog.destroy()

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
add comment

1 Answer 1

up vote 2 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:
        Gtk.main_quit()

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

Your Answer

 
discard

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.