perl tk call on close

R

Robin

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....
Thanks,
-Robin
 
J

Jens Thoms Toerring

Robin said:
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....

If you have

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

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
 
J

Jens Thoms Toerring

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

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
this:

$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 comp.lang.perl.tk newsgroup which might
be an even better place to go to when you have questions about
Perl Tk.
Regards, Jens
 
R

Robin

If you have

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

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



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
finished?



-Robin
 
J

Jens Thoms Toerring

Robin said:
I will look up the docs for the protocol sub.

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
default.
Regards, Jens
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top