perltk-toolkittkx

How do I intercept the event when the user clicks the "close" button in a toplevel window?


When the user clicks the system-provided close button in a toplevel window, I need to free resources.

The question: how do I intercept this event, so that I can call some of my own code?

I've looked through the docs, but can't find anything about destroying toplevel windows.


Solution

  • use warnings;
    use strict;
    use Tkx;
    
    my $mw = Tkx::widget->new('.');
    $mw->g_wm_protocol('WM_DELETE_WINDOW' => \&cleanUp);
    
    Tkx::MainLoop();
    
    sub cleanUp
    {
       print "Cleaning things up\n";
       exit;
    }