Damian Chrzanowski
Damian Chrzanowski

Reputation: 479

Python GTK 3+ controlling closing the window

I am working on a database gtk3 based program. OS used is Linux Mint 17.3 64-bit and Win 7 64 bit. Issue tested under Python 2.7. I cannot seem to be able to control closing the main window. I want to prevent the window from being closed if there are unsaved changes. I use:

self.window.connect("delete-event", self.quit)

The "destroy" event seems completely uncontrollable. From what I understand "delete-event" is supposed to be a request, therefore controllable. So then self.quit looks like this:

def quit(self, widget, something):
    check_before_quit()

So then check_before_quit() checks if there are any unsaved changes. Posts the dialog and the main window is still behind. Code is set to launch Gtk.main_quit() if OK is pressed, otherwise just return. At this point regardless of whether I press ok/cancel or close the dialog, the main window closes anyway! Same thing happens under Win 7 and Linux. I have a bind under Ctrl+Q to Quit. This seems to work fine, asks, and correctly quits or stays in the program. So does pressing the close window button somehow overrides something?

Upvotes: 2

Views: 2082

Answers (1)

zondo
zondo

Reputation: 20336

The delete-event handler should return True or False, not do the quitting itself. It should return True if you don't want to close the window, and False if you do. That sounds a little backwards, so I'll word it differently: It should return True if you want to stop the signal right there, or False if you don't want to do anything about it.

Upvotes: 5

Related Questions