Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > perl tk call on close

Reply
Thread Tools

perl tk call on close

 
 
Robin
Guest
Posts: n/a
 
      11-14-2009
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
 
Reply With Quote
 
 
 
 
Jens Thoms Toerring
Guest
Posts: n/a
 
      11-14-2009
Robin <(E-Mail Removed)> wrote:
> 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
--
\ Jens Thoms Toerring ___ http://www.velocityreviews.com/forums/(E-Mail Removed)
\__________________________ http://toerring.de
 
Reply With Quote
 
 
 
 
Jens Thoms Toerring
Guest
Posts: n/a
 
      11-14-2009
Jens Thoms Toerring <(E-Mail Removed)> wrote:
> Robin <(E-Mail Removed)> wrote:
> > 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....


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
--
\ Jens Thoms Toerring ___ (E-Mail Removed)
\__________________________ http://toerring.de
 
Reply With Quote
 
Robin
Guest
Posts: n/a
 
      11-14-2009
On Nov 14, 2:30*am, (E-Mail Removed) (Jens Thoms Toerring) wrote:
> Robin <(E-Mail Removed)> wrote:
> > 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
> --
> * \ * Jens Thoms Toerring *___ * * *(E-Mail Removed)
> * *\__________________________ * * *http://toerring.de




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
 
Reply With Quote
 
Jens Thoms Toerring
Guest
Posts: n/a
 
      11-14-2009
Robin <(E-Mail Removed)> wrote:
> 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
--
\ Jens Thoms Toerring ___ (E-Mail Removed)
\__________________________ http://toerring.de
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to close a TCP socket? (TCPSocket#close doesn't close it) IƱaki Baz Castillo Ruby 7 01-12-2010 01:32 PM
close all child windows when close the main window jrefactors@hotmail.com HTML 6 08-07-2008 09:10 AM
Why does JdbcRowSetImpl.close() close the database connection? Paul van Rossem Java 0 04-07-2005 07:01 PM
JavaMail POP3 folder.close() method close also store! Daniel Albisser Java 1 04-07-2004 03:45 PM
How to close child browser while parent close? Denon ASP .Net 1 11-14-2003 08:14 AM



Advertisments