perl tk call on close

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

anyone know how you can make a tk perl program call a subrountine when
you close the MainWindow? I looked all over the web and perldocs but
couldn't find how....

Re: perl tk call on close

Quoted text here. Click to load it

If you have

use Tk;
my $main = MainWindow->new( );
$main->protocol( 'WM_DELETE_WINDOW' => sub { print "XXX\n" } );

then trying to close the window should result in "XXX" getting
printed out instead of closing the window (you would need to
add "$main->destroy" to the subroutine to be executed in order
to get the window also closed). But note that this only works
when the window gets closed in a "gentle" way, i.e. by the win-
dow manager sending it a message asking it to destroy itself
(but I guess that's what most "close buttons" do).

                           Regards, Jens
  \   Jens Thoms Toerring  ___

Re: perl tk call on close

Quoted text here. Click to load it

Some more: with

Quoted text here. Click to load it

the sub will only execute when the close button (or something
similar) of the window manager is used to close the window but
not when you you call "$main->destroy".

If you want to get a sub invoked whenever the window is closed
whatever way you instead should bind the <Destroy> event like

$main->bind( '<Destroy>' => sub { print "YYY\n" if $_[0] == $main } );

Note the check for the first argument the function gets passed:
the sub gets called for each subwidget in the window as it is
destroyed and the first argument is the object getting destroyed.
Thus if you want things to get run once instead for each sub-
widget then you have to return from the sub immediately unless
the argument is the main window.

BTW, there's also the newsgroup which might
be an even better place to go to when you have questions about
Perl Tk.
                            Regards, Jens
  \   Jens Thoms Toerring  ___

Re: perl tk call on close

On Nov 14, 2:30=A0am, (Jens Thoms Toerring) wrote:
Quoted text here. Click to load it

Thanks for your reply.
It probably will help alot.

I will look up the docs for the protocol sub. I am trying to design a
html editor. Do you want me to mail you the source code when I get it


Re: perl tk call on close

Quoted text here. Click to load it

There's not too much I found, all I did see is from

perldoc Tk::Wm

As far as I understand it the window manager sends a message of
type WM_DELETE_WINDOW to the application (notifying it via an
event of type ClientMessage) when the "close button" is clicked
on (or something similar tells the window manager that the user
wants the window removed) and the application can react to this
message. Most applications just react by to closing the window
(and exiting if this was their only window) but they can do
whatever they want and using

$toplevel->protocol( 'WM_DELETE_WINDOW => sub{ } );

installs a handler for this message to be used instead of the
                              Regards, Jens
  \   Jens Thoms Toerring  ___

Site Timeline