views:

154

answers:

1

I'm trying to keep a Toplevel window from being closed in OSX by intercepting window manager WM_DELETE_WINDOW event.

#!/usr/bin/env python

from Tkinter import *

def speak():
    print "woof"

root = Tk()
root.title("root")
win = Toplevel()
win.title("win")
win.protocol('WM_DELETE_WINDOW', speak)
root.mainloop()

When I run this I get two pop up windows titled "root" and "win". If I click on the red "x" close button on "win" to close the window, prints "woof" and then closes. However, if I run this same code on windows "win" stays open and keeps printing "woof" every time I click the red "x" close button.

How can I keep the Toplevel window from closing on OSX when I click the red "x" close button?

A: 

You can read about this here in the Tkinter manual:

http://www.pythonware.com/library/tkinter/introduction/events-and-bindings.htm

Search for: "Once you have installed your own handler, Tkinter will no longer automatically close the window."

If it does not help on Mac, that's probably a bug.

This FAQ entry suggests the same solution, but it is not Python:

http://tcl.sourceforge.net/faqs/tk/#wm/delete

fviktor
I tried running the example from the pythonware site and I got the same behavior (choosing cancel still closes the window). There is definitely a discrepancy between Windows and OSX, unless I'm really missing something here... Can somebody else with (snow) leopard try out the WM_DELETE_WINDOW example on the pythonware link above and let me know if they see the same behavior?
cdwilson