Detect TKinter window being closed?

Discussion in 'Python' started by Glen, Dec 2, 2005.

  1. Glen

    Glen Guest

    Is it possible to to detect a Tkinter top-level window being closed with the
    close icon/button (top right), for example to call a function before the
    window actually closes?

    Python 2.4 / Linux (2.6 kernel) if that makes any difference.
    Any info would be greatly appreciated.
    Thanks
    Glen
    Glen, Dec 2, 2005
    #1
    1. Advertising

  2. Glen

    Adonis Guest

    Glen wrote:
    > Is it possible to to detect a Tkinter top-level window being closed with the
    > close icon/button (top right), for example to call a function before the
    > window actually closes?
    >
    > Python 2.4 / Linux (2.6 kernel) if that makes any difference.
    > Any info would be greatly appreciated.
    > Thanks
    > Glen


    Here is an example code taken from:
    http://www.pythonware.com/library/tkinter/introduction/events-and-bindings.htm
    (located at very end)

    Example 7-2. Capturing destroy events

    # File: protocol1.py

    from Tkinter import *
    import tkMessageBox

    def callback():
    if tkMessageBox.askokcancel("Quit", "Do you really wish to quit?"):
    root.destroy()

    root = Tk()
    root.protocol("WM_DELETE_WINDOW", callback)

    root.mainloop()

    Hope this helps.

    Adonis
    Adonis, Dec 2, 2005
    #2
    1. Advertising

  3. Fredrik Lundh, Dec 2, 2005
    #3
  4. Glen

    Glen Guest

    Thanks Fredrik and Adonis that's just what I needed, plus a bit more to
    learn about.
    Glen, Dec 2, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?R2FyeQ==?=
    Replies:
    1
    Views:
    498
    =?Utf-8?B?R2FyeQ==?=
    Nov 11, 2005
  2. news.microsoft.com
    Replies:
    2
    Views:
    477
    news.microsoft.com
    Dec 17, 2005
  3. Matt Kruse
    Replies:
    5
    Views:
    287
    Richard Cornford
    Sep 9, 2003
  4. Kiki
    Replies:
    3
    Views:
    167
    kiki christie
    Jul 28, 2004
  5. TM360
    Replies:
    2
    Views:
    181
    TM360
    Jun 30, 2005
Loading...

Share This Page