perl tk call on close

Discussion in 'Perl Misc' started by Robin, Nov 14, 2009.

  1. Robin

    Robin Guest

    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
    Robin, Nov 14, 2009
    #1
    1. Advertising

  2. Robin <> 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://toerring.de
    Jens Thoms Toerring, Nov 14, 2009
    #2
    1. Advertising

  3. Jens Thoms Toerring <> wrote:
    > Robin <> 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 ___
    \__________________________ http://toerring.de
    Jens Thoms Toerring, Nov 14, 2009
    #3
  4. Robin

    Robin Guest

    On Nov 14, 2:30 am, (Jens Thoms Toerring) wrote:
    > Robin <> 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://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
    Robin, Nov 14, 2009
    #4
  5. Robin <> 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 ___
    \__________________________ http://toerring.de
    Jens Thoms Toerring, Nov 14, 2009
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Denon
    Replies:
    1
    Views:
    571
    Saravana [MVP]
    Nov 14, 2003
  2. Daniel Albisser
    Replies:
    1
    Views:
    1,073
    GaryM
    Apr 7, 2004
  3. Paul van Rossem
    Replies:
    0
    Views:
    601
    Paul van Rossem
    Apr 7, 2005
  4. Replies:
    6
    Views:
    8,831
    sateeshranga
    Aug 7, 2008
  5. Iñaki Baz Castillo
    Replies:
    7
    Views:
    845
    Iñaki Baz Castillo
    Jan 12, 2010
Loading...

Share This Page