in reply to closing window and running sub-routine

I think you want to recheck the Tk::callbacks manpage:
-command => sub{ ... } -command => [ \&subname, args ... ]